Question: geom_bar plot with several variables
4
gravatar for jordi.planells
22 months ago by
jordi.planells270 wrote:

Hi all, I need your help. I am struggling on getting a bar plot with ggplot2 package.

Imagine I have 3 different variables (which would be my y values in aes) that I want to plot for each of my samples (x aes):

 str(df) 
'data.frame':   4 obs. of  4 variables:
 $ samples: Factor w/ 4 levels "S1","S2","S3",..: 1 2 3 4
 $ v1     : int  234 189 167 235
 $ v2     : int  345 265 235 254
 $ v3     : int  54 48 32 645

I would like to have a barplot in which I have a group per sample (4), each of them with 3 bars, one for each variable (12 bars in total). I know that if I want to extract the height from the df I have to call for stat = "identity". So far this has been my guess, but it's not working properly:

n <- ggplot(df) + 
  geom_bar(mapping = aes(x = samples, y = v1), stat = "identity", position = "dodge") + 
  geom_bar(mapping = aes(x = samples, y = v2), stat = "identity", position = "dodge") +
  geom_bar(mapping = aes(x = samples, y = v3), stat = "identity", position = "dodge")

Could you please help me?

Thanks before hand

ggplot2 R • 26k views
ADD COMMENTlink modified 17 months ago by gpushpaganesan0 • written 22 months ago by jordi.planells270

4 observation of 4 variables: I have Type position in more variable (20),how to give minimum variable(5)

ADD REPLYlink written 17 months ago by gpushpaganesan0

Excuse me...?

ADD REPLYlink written 17 months ago by Kevin Blighe67k
6
gravatar for cpad0112
22 months ago by
cpad011214k
Hyderabad India
cpad011214k wrote:

with iris data : 4 observation of 4 variables:

data(iris)
library(ggplot2)
library(tidyr)

iris %>%
  gather("Type", "Value",-Species) %>%
  ggplot(aes(Species, Value, fill = Type)) +
  geom_bar(position = "dodge", stat = "identity") +
  theme_bw()

Rplot

Separate boxes for each species:

iris %>%
  gather("Type", "Value",-Species) %>%
  ggplot(aes(Species, Value, fill = Type)) +
  geom_col(position = "dodge") +
  theme_bw()+
  facet_wrap(~Species,scales = "free_x")

Rplot01

input (for one species, likewise for rest two species):

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
ADD COMMENTlink modified 22 months ago • written 22 months ago by cpad011214k

Hi, Wonderful explanations. Thanks! But like to know how to rearrange the variables in the each box. for example, petal.length, sepal.length, petal.width, sepal.width

Also how about display in descending order. Thanks

ADD REPLYlink written 14 months ago by Sam20

You can re-order these by converting them to [ordered] factors in the input data.

ADD REPLYlink modified 4 months ago • written 4 months ago by Kevin Blighe67k

look at the bottom figures of https://digibio.blogspot.com/2020/07/control-grouped-barcharts-in-ggplot.html

ADD REPLYlink written 4 months ago by cpad011214k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1243 users visited in the last hour