How to add geom_col to my code appropriately

145 Views Asked by At
datapasta::df_paste(head(df,45)[,c('A.','Object','Colour')])
#> Error in unclass(x)[...]: subscript out of bounds


data.frame(
  stringsAsFactors = FALSE,
                               A. = c(113,34,NA,9,11,28,41,66,12,23.2,
                                      22.8,21.7,22.7,22.2,21.7,22.5,23.4,
                                      NA,23.6,21.3,22.8,17.8,23.1,23.5,
                                      22.3,25.6,23.6,23.7,23.9,22.6,22.9,
                                      23.7,26.2,24.3,21.9,22.5,25.2,22.6,
                                      24.1,26.1,23,24,23.9,26.7,NA),
                           Object = c("Bag","shoe","shoe","belt","shoe",
                                      "belt","belt","Bag","Bag","belt","belt",
                                      "Bag","Bag","shoe","shoe","belt",
                                      "shoe","belt","belt","Bag","Bag","belt",
                                      "Bag","shoe","shoe","belt","shoe",
                                      "belt","belt","Bag","shoe","shoe","belt",
                                      "shoe","belt","belt","Bag","shoe",
                                      "shoe","belt","shoe","belt","belt",
                                      "Bag",NA),
                           Colour = c("red","black","black","red","black",
                                      "blue","blue","red","blue","red","red",
                                      "blue","red","black","black","red",
                                      "black","blue","blue","red","red",
                                      "black","black","red","black","blue",
                                      "blue","red","red","black","black","red",
                                      "black","blue","blue","red","red",
                                      "black","black","red","black","blue",
                                      "blue","red",NA)
               )
#>       A. Object Colour
#> 1  113.0    Bag    red
#> 2   34.0   shoe  black
#> 3     NA   shoe  black
#> 4    9.0   belt    red
#> 5   11.0   shoe  black
#> 6   28.0   belt   blue
#> 7   41.0   belt   blue
#> 8   66.0    Bag    red
#> 9   12.0    Bag   blue
#> 10  23.2   belt    red
#> 11  22.8   belt    red
#> 12  21.7    Bag   blue
#> 13  22.7    Bag    red
#> 14  22.2   shoe  black
#> 15  21.7   shoe  black
#> 16  22.5   belt    red
#> 17  23.4   shoe  black
#> 18    NA   belt   blue
#> 19  23.6   belt   blue
#> 20  21.3    Bag    red
#> 21  22.8    Bag    red
#> 22  17.8   belt  black
#> 23  23.1    Bag  black
#> 24  23.5   shoe    red
#> 25  22.3   shoe  black
#> 26  25.6   belt   blue
#> 27  23.6   shoe   blue
#> 28  23.7   belt    red
#> 29  23.9   belt    red
#> 30  22.6    Bag  black
#> 31  22.9   shoe  black
#> 32  23.7   shoe    red
#> 33  26.2   belt  black
#> 34  24.3   shoe   blue
#> 35  21.9   belt   blue
#> 36  22.5   belt    red
#> 37  25.2    Bag    red
#> 38  22.6   shoe  black
#> 39  24.1   shoe  black
#> 40  26.1   belt    red
#> 41  23.0   shoe  black
#> 42  24.0   belt   blue
#> 43  23.9   belt   blue
#> 44  26.7    Bag    red
#> 45    NA   <NA>   <NA>


df.summary <- df %>%
  group_by(Object,Colour) %>%
  summarise(
    mn = mean(A., na.rm = TRUE),
    sd = sd(A., na.rm = TRUE)
  )
#> Error in df %>% group_by(Object, Colour) %>% summarise(mn = mean(A., na.rm = TRUE), : could not find function "%>%"
#> `summarise()` has grouped output by 'Object'. You can override using the `.groups` argument.
ggplot(df.summary, aes(Object, mn)) +
  geom_errorbar(
    aes(ymin = mn-sd, ymax = mn+sd, color = Colour),
    position = position_dodge(0.3), width = 0.2
  ) +
  geom_point(aes(color = Colour), position = position_dodge(0.3)) +
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#00FF00")) +
  geom_line(aes(group = Colour),data = df.summary)+
  geom_col(fill = alpha("#2C77BF", .3))+
geom_col(aes(y = A.),fill = alpha("#2C77BF", .009), position = position_dodge(0.3), data= df)
#> Error in ggplot(df.summary, aes(Object, mn)): could not find function "ggplot"

Created on 2022-05-04 by the reprex package (v2.0.1)

This image style is what I am targeting to achieve Target image

But this is an example of the image I have gotten from my codes this is image of my code

I have a target plot I want to achieve, and that is what has been uploaded as Target (1). I have used my codes to create my plot but the bars are fused together with others than individually. Can you help with the codes for the geom_col(). I am thinking that is where I am getting wrongly

1

There are 1 best solutions below

1
stefan On

The main issue with your code is that you used the wrong dataset for the bars and the points. As the bars reflect the mean use df.summary for geom_col and as the points reflect the raw data use df for geom_point. Additionally I use position_jitterdodge for the points to avoid overplotting and added an position_dodge for the lines.

library(ggplot2)
library(dplyr)

df.summary <- df %>%
  group_by(Object, Colour) %>%
  summarise(
    mn = mean(A., na.rm = TRUE),
    sd = sd(A., na.rm = TRUE)
  ) |> 
  tidyr::drop_na()

ggplot(df.summary, aes(Object, mn)) +
  geom_col(aes(fill = Colour), position = position_dodge(.6), alpha = .5, width = .6) +
  geom_errorbar(
    aes(ymin = mn - sd, ymax = mn + sd, color = Colour),
    position = position_dodge(0.6), width = 0.2
  ) +
  geom_point(aes(y = A., color = Colour), data = df, position = position_jitterdodge(dodge.width = 0.6, jitter.width = .1), na.rm = TRUE) +
  geom_line(aes(color = Colour, group = Colour), position = position_dodge(.6)) +
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#00FF00"), aesthetics = c("color", "fill"))