About creating multi variable bar chart using ggplot

I have this data

data frame name is "chartT"

Trying to make a barplot for variables TA, TQ & TC values against Subject column in one single chart.

I am doing this

ggplot(chartT)+
geom_bar(aes(x=TQ,y=Subject),stat="identity",position="dodge)+
geom_bar(aes(x=TA,y=Subject),stat = "identity",position = "dodge")+
geom_bar(aes(x=TC,y=Subject),stat="identity",position = "dodge")+
theme(axis.text = element_text(angle = 90,hjust = 1))+facet_wrap(TQ~TA~TC)

Output generated is unreadable.

Thanks for any advice provided.

Hi
I'm not entirely sure what you're trying to plot, could you be looking for geom_col()?
Here's an attempt, let me know if this isn't what you were trying to do. Note: you don't need position = "dodge" when you're going to facet_wrap at the end anyway.

On a side note, its easier to get help (and to help) if you provide a reprex.

library(tidyverse)

Df <- tibble(Subject = letters[1:19],
             TQ = runif(19, 1, 505),
             TA = runif(19, 1, 273),
             TC = runif(19, 1, 194))

Df <- Df %>% 
  gather(keys, values, TQ:TC)

ggplot(Df, aes(Subject, values)) +
  geom_col(aes(fill = keys)) +
  facet_wrap(~ keys) +
  coord_flip()

Created on 2018-11-27 by the reprex package (v0.2.1)

5 Likes

Actually, I wanted to plot the same but, the bar plot should be in one single chart side by side, for each subject. I am able to plot stacked bar charts but not multi bar chart("Clustered Column " in words of Excel).

Something like this,

Thank You for the help, and I am a newbie in R. Sorry, for silly mistakes.

library(tidyverse)

Df <- tibble(Subject = letters[1:19],
             TQ = runif(19, 1, 505),
             TA = runif(19, 1, 273),
             TC = runif(19, 1, 194))

Df <- Df %>% 
  gather(keys, values, TQ:TC)

ggplot(Df, aes(keys, values)) +
  geom_col(aes(fill = keys)) + 
  facet_wrap(~Subject, ncol = 4)

Created on 2018-11-28 by the reprex package (v0.2.1)

1 Like

Thank You, needed something like this. Just, out of curiosity, is it possible to merge all the subjects into one chart.
i.e. x axis will contain subjects, y axis values and colour will be the variables(TA,TC,TQ).

You can do it easily with ggplot since you have mapped it. It is basicly like @bragks' solution, except that you don't need coord_flip() and facet_wrap().

library(tidyverse)

Df <- tibble(Subject = letters[1:19],
             TQ = runif(19, 1, 505),
             TA = runif(19, 1, 273),
             TC = runif(19, 1, 194))

Df <- Df %>% 
  gather(keys, values, TQ:TC)

ggplot(Df, aes(x = Subject, y = values)) +
  geom_col(aes(fill = keys))
1 Like

If I do that the single chart plotted is giving the sum of Questions for all the subject. and all subjects are taken as one. The values are summed for all the subjects w.r.t TQ,TA,TC. But, what is needed is plotting TQ,TA & TC for all subjects, in one axis (similar to excel chart).

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

install.packages("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.