Home > Mobile >  How to display different colors in geom_point?
How to display different colors in geom_point?

Time:05-13

This is my dataframe:

df = structure(list(qCountry = c("AT", "DE", "ES", "FI", "FR", "GR", 
"HU", "IR", "IT", "LV", "NL", "POL", "PT", "RO", "SWE", "EU"), 
    Mean = c(1.34199395770393, 1.37664132688321, 1.29144095341278, 
    1.42088404868674, 1.45019920318725, 1.29786200194363, 1.24528301886792, 
    1.26937046004843, 1.38345864661654, 1.39706780696396, 1.38751714677641, 
    1.30804248861912, 1.28609062170706, 1.2320819112628, 1.32588699080158, 
    1.33425470556542), Sd = c(0.474520963779525, 0.484711259139164, 
    0.454549282145671, 0.493859200850428, 0.497678958439859, 
    0.45742966912063, 0.430377930416405, 0.443767080631815, 0.48638085640142, 
    0.489439780963027, 0.487350499450418, 0.461801022550429, 
    0.452051373271304, 0.422281105345888, 0.468859354590332, 
    0.467003889139754)), row.names = c(NA, -16L), class = c("tbl_df", 
"tbl", "data.frame"))

I then plot it as follows:

c5_1 %>%
  ggplot(aes(x = reorder(qCountry,-Mean), y = Mean, group = qCountry))   
  geom_point(size = 3.5)  
  geom_errorbar(aes(ymin=Mean-Sd, ymax=Mean Sd))   
  labs(title = "Some Title", subtitle =  "Some subtitle",x = "", y="")   
  scale_y_continuous(breaks = c(1, 2), 
                     labels = c("Yes", "No"), limits = c(0.8, 2))  
  coord_flip()   
  theme_classic()

The issue with this is that I would like "EU" only to be displayed in red, while the rest stays black.

Can anyone help me with that?

Thanks!

CodePudding user response:

You can create a new column with the color name and call scale_color_identity():

df %>%
  mutate(color_plot = ifelse(qCountry == "EU", "red", "black")) %>% 
  ggplot(aes(x = reorder(qCountry,-Mean), y = Mean, group = qCountry, 
             color = color_plot))   
  geom_point(size = 3.5)  
  geom_errorbar(aes(ymin=Mean-Sd, ymax=Mean Sd))   
  labs(title = "Increasing 1% of your personal tax bill for financing financial support to other EU member states", subtitle =  "Natural Disaster",x = "", y="")   
  scale_y_continuous(breaks = c(1, 2), 
                     labels = c("Yes", "No"), limits = c(0.8, 2))  
  coord_flip()   
  scale_color_identity()  
  theme_classic()

Output is:

enter image description here

  • Related