Isma Soto Almena
Isma Soto Almena

Reputation: 227

Change colors in a basic geom plots

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

Upvotes: 2

Views: 265

Answers (2)

Rui Barradas
Rui Barradas

Reputation: 76402

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

Upvotes: 1

KacZdr
KacZdr

Reputation: 1564

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

Upvotes: 1

Related Questions