0

I have a dataframe that has 2 rows and 3 columns. I want to be able to chart all three columns, and it would probably make the most sense in a geom_col or geom_bar chart. The x axis would stay the same for both graphs.

Dataframe has columns a1, b1, b2

I used this template to create one graph. Hope this amount of detail is helpful.

Datafame %>% 
  ggplot(aes(x = a1, y = b1, fill = b1)) +
geom_col(position = "dodge", show.legend = FALSE) +
  theme(axis.text.x = element_text(angle = 90)) +
geom_text(aes(label = b1), hjust = -.5, vjust = -.5, size = 3.5) +
  expand_limits(x = 2, y = .35) +
  coord_flip()
mrm081
  • 13
  • 2

1 Answers1

1

you can do it with ggplot if you add individual 'aes()' asthetics to different geoms .

library(ggplot2)

df<-data.frame(a1 = c(1:10), b1 = rnorm(10,5), b2= rnorm(10, 3), b3= rnorm(10, 1))

ggplot(df)+
  geom_line(aes(x = a1,  y = b1 ))+
  geom_point(aes(x = a1, y = b2, color = "red" ))+ 
  geom_col(aes(x = a1, y = b3 ))

See the three different geoms under one Xaxis (x = a1):

enter image description here

Adding Dave2e 's valueable comment

if you want to have multiple geom_cols , you wiil have to transform you data first to longer format.

library(tidyr)
df %>% pivot_longer(b1:b4) %>% ggplot() +
  geom_col(aes(x= a1, y=value, fill= name ), position = "dodge")

enter image description here

user12256545
  • 2,755
  • 4
  • 14
  • 28