melbez
melbez

Reputation: 1000

Using group argument in aes() in ggplot2

I am trying to use the "group" argument in aes() in ggplot2, and I am not sure why it is not working as I currently have it.

I would like an image that groups my "maskalthalf" variable in the way that this image uses "sex" (found here).

enter image description here

This is what my graph currently looks like.

enter image description here

This is the code I have so far.

ggplot(groups, aes(x = message, y = mean, group = factor(maskalthalf))) + 
  geom_bar(stat = "identity", width = 0.5, fill = "003900") +
  geom_text(aes(label = round(mean, digits = 1), vjust = -2)) + 
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
  labs(title = "Evaluations of Personal and General Convincingness") + 
  ylab("Rating") + 
  xlab("Personal evaluation or general evaluation") + 
  ylim(0, 8)

This is a sketch of what I am aiming for:

enter image description here

Data:

structure(list(maskalthalf = c("High", "High", "Low", "Low"), 
    message = c("General", "Personal", "General", "Personal"), 
    mean = c(4.79090909090909, 6.38181818181818, 4.69879518072289, 
    4.8433734939759), se = c(0.144452868727642, 0.104112130946133, 
    0.149182255019704, 0.180996951567937)), row.names = c(NA, 
-4L), groups = structure(list(maskalthalf = c("High", "Low"), 
    .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Upvotes: 0

Views: 1162

Answers (1)

neilfws
neilfws

Reputation: 33802

The image in your first example uses facets to group by variable. So you could try that:

ggplot(df1, aes(x = message, y = mean)) + 
  geom_col(width = 0.5, fill = "003900") +
  geom_text(aes(label = round(mean, digits = 1), vjust = -2)) + 
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) + 
  labs(title = "Evaluations of Personal and General Convincingness") + 
  ylab("Rating") + 
  xlab("Personal evaluation or general evaluation") + 
  ylim(0, 8) +
  facet_wrap(~maskalthalf)

enter image description here

Upvotes: 1

Related Questions