 # Plot using ggplot and grouping

Hi I have a table like this:

Question Age_Group Value
Question 1 10-20 15
Question 2 10-20 16
Question 3 10-20 12
.....
Question 1 20-30 13
Question 2 20-30 15
Question 3 20-30 18

I want to build a histogram where questions are in y - axis, age groups in x - axis and by bars the comparision is visible based on Value column.

How to make this possible?
Regards

Not sure how you want to put the question on the y-axis and make "comparison visible based on Value".

Intuitively, I would plot data like that as a barplot, with Age and Question on the x axis and Value on the y axis.

``````dta <- tribble(~Question, ~Age_Group, ~Value,
"Question 1", "10-20", 15,
"Question 2", "10-20", 16,
"Question 3", "10-20", 12,
"Question 1", "20-30", 13,
"Question 2", "20-30", 15,
"Question 3", "20-30", 18)

ggplot(dta) +
geom_col(aes(x=Age_Group, y=Value, fill=Question), position = "dodge")
``````

If you really want your Questions on the y axis, then some kind of dotplot could work:

``````ggplot(dta) +
geom_point(aes(x=Age_Group, y=Question, fill=Value, size=Value))
``````

I don't understand how you would have a histogram or barplot with the Question on the y axis.

1 Like

Working great.
Thank you

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

When you say it didn't work, do you mean the code failed, or it works but the result is not what you need?

Because if I understand correctly, the first code block I suggested is close to what you want.

Unless you need the bars stacked:

``````ggplot(dta) +
geom_col(aes(x=Age_Group, y=Value, fill=Question), position = "stack")
``````

1 Like