Help with creating ggplot

Hi there!

I would like to ask how can I create specific colour for each data frame. In this code below, I want to illustrate the correlations between utopian thinking and each of the wellbeing subscale (Autonomy, Environmental_Mastery, Personal_Growth, Positive_Relations, Purpose_in_life, Self_acceptance) in Carol Ryff's wellbeing scale. Since i want each subscale to have a specific color for its datapoint but I can only do so with the best fitted lines. I would like to know is there specific argument to help me with that?

Much appreciated.
Nam.

Here is the code:

UW_long <- recode_UW %>%
select(c(Utopianism, Autonomy, Environmental_Mastery, Personal_Growth, Positive_Relations, Purpose_in_life, Self_acceptance)) %>%
pivot_longer(cols=c(Autonomy, Environmental_Mastery, Personal_Growth, Positive_Relations, Purpose_in_life, Self_acceptance), names_to="wellbeing_subscale", values_to="scores")

#Figure 7
UW_long_corrplot <- UW_long %>%
ggplot(aes(x=Utopianism, y=scores, fill=wellbeing_subscale,show.legend=FALSE)) +
geom_point(show.legend=FALSE) +
facet_wrap(facets = ~ wellbeing_subscale) +
theme_classic() +
#theme(strip.background=element_rect(fill="white")) +
labs(x="Utopianism",
y="Wellbeing scores (Wellbeing subScales)") +
geom_smooth(method = "lm", se = TRUE)
plot(UW_long_corrplot)

Here is the graph

Hard to answer definitely without a reproducible example since you didn't provide test data, but I think you'll get the desired result by using the aesthetic color for the geom_point:

...
ggplot(aes(x=Utopianism, y=scores, fill=wellbeing_subscale,show.legend=FALSE)) +
geom_point(aes(color = wellbeing_subscale, show.legend=FALSE) +
...

The point is that fill applies to surfaces (e.g. the smooth of the fitted line), but color applies to the dots and lines.

Also that means if you gave color in the ggplot() command, it would apply to both the points and the fitted line (which is currently blue).

ggplot(aes(x=Utopianism, y=scores, fill=wellbeing_subscale, color = wellbeing_subscale, show.legend=FALSE)) +

Thank you so much Alexis, I have one more question though. How can i remove the legend from the graph. Although I have added show.legend=FALSE in ggplot, geom_point, but the legend still appear. Is there any argument that I can add that would remove the legend.

Thank you so much,
Nam.

You are right to use show.legend = FALSE, but currently you used it inside aes(). So you get this warning:

Warning message:
Ignoring unknown aesthetics: show.legend 

and the legends are shown. This is because inside aes(), ggplot expects aesthetic mappings but this is a more general parameter, it's not about the data. So it has to be outside aes().

Here is a reproducible example with fake data:

library(tidyverse)

n <- 20
slopes <- c("autonomy" = 0, "environment" = -.1, "personal" = .1)
offsets <- c("autonomy" = 30, "environment" = 25, "personal" = 40)

tibble(utopianism = rep(1:7, times = 3*n) + rnorm(7*3*n, mean = 0, sd = .7),
       wb_subscale = rep(c("autonomy","environment","personal"), each = 7*n),
       scores = slopes[wb_subscale]*utopianism + offsets[wb_subscale] +
         rnorm(7*3*n, mean = 0, sd = 3)) |>
  ggplot(aes(x=utopianism, y=scores)) +
  theme_classic() +
  geom_point(aes(color = wb_subscale), show.legend=FALSE) +
  facet_wrap(~wb_subscale) +
  geom_smooth(method = "lm", se = TRUE, color = 'black', show.legend=FALSE)
#> `geom_smooth()` using formula 'y ~ x'

Created on 2022-10-03 by the reprex package (v2.0.1)

You can try playing with this example: if you put show.legend in ggplot() but outside its aes(), it's equivalent to indicating it both in geom_point() and geom_smooth(). But if you put it inside the aes() parentheses, it will be ignored because it is not an aesthetic.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.