-1

I uploaded my data from a excelsheet into R and from that Data I want to get a grouped bar/column chart between the value “mean value” and “Größe”. However the dodge function shows me this kind of graphic:

enter image description here

Can somebody help me how to get a grouped bar chart between two values? Thanks a lot

This is my Code:

library( ggplot2)

ggp<-ggplot(data=BeispielR, aes (x=Namen, y=`Mean value`, fill=Größe))+
  geom_col (position="dodge")

I Also tried the stat="identity" addition to geom_col, but it did not work out.

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Welcome to SO! In general your code looks fine and should give you the desired result. For more help we need [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. To share your data use e.g. `dput(BeispielR[1:10, ])` for the first ten rows of data. Also, your post does not include the mentioned "kind of graphic". – stefan Apr 16 '23 at 11:24
  • An image of your data isn't that helpful. To share your data include it directly using e.g. `dput()` as I suggested in my first comment. Moreover, I don't know how a grouped barchart should like for your data. In general a grouped barchart requires a categorical variable which defines the groups like e.g. gender whereas your variable `Größe` is a continuous variable. – stefan Apr 16 '23 at 12:03
  • 1
    Try `fill=as.factor(Größe)`. – Rui Barradas Apr 16 '23 at 13:21

1 Answers1

0

You cannot apply this when you have a continuous variable. I tried to somewhat replicate what I think you want to do. I created height categories and then applied the color argument on them.

library(tidyverse)

set.seed(123)

height <- sample(150:180,30, replace = T)
name <- rep(c("A", "B", "C"), 10)
value <- sample(40:90, 30, replace = T)

df <- data.frame(name, height, value)

df %>%
  arrange(name, height)
#>    name height value
#> 1     A    152    44
#> 2     A    154    81
#> 3     A    156    52
#> 4     A    163    56
#> 5     A    167    51
#> 6     A    171    48
#> 7     A    174    64
#> 8     A    176    62
#> 9     A    176    66
#> 10    A    180    58
#> 11    B    152    82
#> 12    B    154    66
#> 13    B    157    47
#> 14    B    158    73
#> 15    B    159    57
#> 16    B    164    75
#> 17    B    169    84
#> 18    B    171    54
#> 19    B    174    80
#> 20    B    174    71
#> 21    C    158    72
#> 22    C    159    78
#> 23    C    160    71
#> 24    C    163    46
#> 25    C    168    53
#> 26    C    168    46
#> 27    C    175    49
#> 28    C    175    51
#> 29    C    177    77
#> 30    C    178    68

df %>%
  mutate(height_fac = case_when(
    between(height, 150, 164) ~ "150-164",
    T ~ "165+"
  )) %>%
  group_by(name, height_fac) %>%
  summarise(mean_val= mean(height)) %>%
  ggplot(aes(x= name, y= mean_val, fill= height_fac))+
  geom_col(position = "dodge")
#> `summarise()` has grouped output by 'name'. You can override using the
#> `.groups` argument.

Created on 2023-04-17 with reprex v2.0.2

Nick Glättli
  • 421
  • 1
  • 7