antonina
antonina

Reputation: 109

bar plot with two seperate bars of df with multiple values

following problem: I have this dataset

df
Ch         V1          V2
A          a1          a2
B          b1          b2
C          c1          c2
....

While V1 and V2 are numerical values. I want to create a barplot with a barplot for each V1 and V2 value. The code I tried

ggplot(data = df %>% gather(Variable, No., -Ch), 
       aes(x = reorder(ID,-No.), y = No., fill = Variable)) + 
  geom_bar(stat = 'identity', position= position_dodge())+
  geom_col()+
  xlab("Section of industry")+
  ylab("No. of occurences")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, size=1),
        plot.title = element_text(hjust = 0.5),
        legend.position = "top")+
  ggtitle("XXX")

Even with using position= position_dodge() it only merges the both bars in one like this: enter image description here Any idea how I can seperate the or why positon_dodge() is not working? I suppose because I used the gather function before?

Another problem is, that the values of Ch are too big, so if I want to display them in a readable manner the graph "disappears". Is there a ways to show these values (maybe write the value in two lines) so that this can be displayed?

Thank you very much!

Upvotes: 1

Views: 108

Answers (1)

dc37
dc37

Reputation: 16178

Your issue is that you have both geom_bar and geom_col in your ggplot call, so the position_dodge() argument in geom_bar is overpass by geom_col producing thus a stacked bar. So, removing geom_col should works. Alternatively, you can remove geom_bar and pass position = position_dodge() to geom_col.

To abbreviate your x labels, you can use scale_x_discrete(abbreviate) as mentioned in this post: How can I shorten x-axis label text in ggplot?

library(dplyr)
library(ggplot2)
library(tidyr)
df %>% pivot_longer(.,-Ch, names_to = "Var", values_to = "Val") %>%
  ggplot(aes(x = Ch, y = Val, fill = Var))+
  geom_col(position = position_dodge())+
  xlab("Section of industry")+
  ylab("No. of occurences")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        plot.title = element_text(hjust = 0.5),
        legend.position = "top")+
  ggtitle("XXX")+
  scale_x_discrete(label = abbreviate)

enter image description here

Example Data

structure(list(Ch = structure(1:5, .Label = c("AAAAAAAAAAAAAA", 
"BBBBBBBBBBBB", "CCCCCCCCCCCCCCCC", "DDDDDDDDDDDDDD", "EEEEEEEEEEEEE"
), class = "factor"), V1 = 1:5, V2 = 5:9), class = "data.frame", row.names = c(NA, 
-5L))

Upvotes: 1

Related Questions