2

everyone, i was trying to change color for the next plot : enter image description here

i have tried other solutions as:

my_pal <- colorRampPalette(c("yellow","firebrick2"))

  scale_color_gradientn(colours = my_pal(6))

or adding color = ... in geom_point , but something is wrong in my code this is a example of my data:

structure(list(Class = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Amphibia", "Reptilia", "Diverse"), scores = structure(c(Amphibia = 0.0171454425174863, 

Diverse = 0.0557016619533333, Reptilia = 0.0306895793776627), .Dim = 3L, .Dimnames = list(
    c("Amphibia", "Diverse", "Reptilia"))), class = "factor"), 
cost_bil = c(0.00061474244, 0.00061474244, 0.00333970653, 
0.00333970653, 0.00038007634, 0.00038007634, 0.00013713485, 
0.00013713485, 0.0005509038, 0.0005509038), value = c("Observed", 
"High", "Observed", "High", "Observed", "High", "Observed", 
"High", "Observed", "High")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

i selected only 10 column so if possible that the results wont be the same, the code that i used is :

p9 <-ggplot(expanded_long, aes(x = Class, y = value)) +
 geom_point(aes(size = cost_bil)) +
 facet_grid(name ~ ., switch = "y", scales = "free_y") +
 scale_size_continuous(range = c(1, 20)) +
 labs(x = "Taxonomic Class", y = NULL, size = "US$ billions", color = "US$ billions") +
 theme(
    panel.spacing.y = unit(0, "pt"),
    strip.placement = "outside",
    strip.background.y = element_blank()
  ) + 
  guides(size=guide_legend(reverse = TRUE)) + 
  theme_classic() + 
  scale_size(range=c(5,20))

All help is aprreciate, i just want change colors for my main graph and for the legend, thanks so much

Martin Gal
  • 16,640
  • 5
  • 21
  • 39

2 Answers2

1

Try the following.
Note that there is no need to define a colours vector, with scale_colour_gradient's arguments low and high the colours will be automatically mapped to th continuous variable cost_bil.

library(ggplot2)


ggplot(expanded_long, aes(x = Class, y = value)) +
  geom_point(aes(size = cost_bil, colour = cost_bil)) +
  scale_size_continuous("US$ billions", range = c(1, 20)) +
  scale_colour_gradient("US$ billions", low = "yellow", high = "firebrick2")+
  labs(x = "Taxonomic Class", y = NULL, 
       size = "US$ billions", colour = "US$ billions") +
  guides(size = guide_legend(reverse = TRUE),
         colour = guide_legend(reverse = TRUE)) + 
  facet_grid(name ~ ., switch = "y", scales = "free_y") +
  theme_classic() + 
  theme(
    panel.spacing.y = unit(0, "pt"),
    strip.placement = "outside",
    strip.background.y = element_blank()
  )

thanks so much, but this is what I got

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
1

First to geom_point() add color=cost_bil, then guides(colour = guide_legend(reverse=F)) after it.

Here You have example plot with same pattern:

ggplot(head(midwest,100), aes(x=area, y=poptotal)) + 
  geom_point(aes(size=area,color=area)) + 
  scale_colour_continuous(low = '#32CD32', high = '#ff4040') +
  guides(colour = guide_legend(reverse=F))

Output: enter image description here

KacZdr
  • 1,267
  • 3
  • 8
  • 23
  • Thanks so much both of you, @Jacek Szyszko but ia m getting like a mix among various colors, i update my answer with a example – Isma Soto Almena Jul 18 '21 at 20:38
  • @Rui Barradas thanks so much for your help, but i get the same error (check new image) – Isma Soto Almena Jul 18 '21 at 20:40
  • @IsmaSotoAlmena That is due to overplotting, the smaller points (`cost_bil`) are plotted over the bigger ones. If the data is ordered by `cost_bil` only the bigger will be visible, but then you will not see the smaller points. Also, please post comments to my answer there, below my answer. – Rui Barradas Jul 18 '21 at 21:05