Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouped Barplot, One numerical vs three factorial variables

Tags:

I'm having issues with the following. I need to barplot 3 factor variables vs 1 numerical variable.

My dataset:

Site,Gall,Status,Count
Site1,absent,unhealthy,35
Site1,absent,healthy,1750
Site1,present,unhealthy,23
Site1,present,healthy,1146
Site2,absent,unhealthy,146
Site2,absent,healthy,1642
Site2,present,unhealthy,30
Site2,present,healthy,333

I have tried using ggplot, but then it only lets me define x, y, and one more option, so I have used fill=Gall.

My code looks as following, I am still missing one factor variable.

ggplot(dat, aes(Status, Count, fill = Gall)) +
  geom_bar(stat = "identity", position = "dodge")

Can anyone help me please ?

Thank you, much appreciated

like image 614
RLover Avatar asked May 08 '17 04:05

RLover


People also ask

What type of bar graph has several categories on the same bar?

A horizontal grouped bar chart is a variant of a bar graph in which multiple data categories are compared and a particular color is used to denote a definite series across all data categories represented. It is also known as a clustered bar graph or a multi-set bar chart.

When should clustered bar charts be used?

A clustered bar chart can be used when you have either: (a) two nominal or ordinal variables and want to illustrate the differences in the categories of these two variables based on some statistic (e.g., a count/frequency, percentage, mean, median, etc.); or (b) one continuous or ordinal variable and two nominal or ...

What type of graph helps in grouping the data and represent the values within each group of data?

A bar graph can be of great use when you have to explain the meaning of complex data. It allows you to compare different sets of data among different groups easily. It instantly demonstrates this relationship using two axes, where the categories are on one axis and the various values are on the other.

Which parameter could be used to convert stacked Barplot into grouped bar plot?

Then the user needs to call the geom_bar() function from the ggplot package with the required parameters into it to create the grouped bar plot in the R programming language. geom_bar() function: This function makes the height of the bar proportional to the number of cases in each group.


1 Answers

There are a couple of solutions. If you are intent on filling by two factors, you can use interaction:

ggplot(dat, aes(Status, Count)) + 
  geom_col(aes(fill = interaction(Site, Gall)), position = "dodge")

enter image description here

In general though, it's better to use faceting for multiple factors. For example:

ggplot(dat, aes(Status, Count)) + 
  geom_col(aes(fill = Gall), position = "dodge") + facet_grid(Site ~ .)

enter image description here

like image 63
neilfws Avatar answered Sep 24 '22 09:09

neilfws