Reputation: 1139
I wish to plot barplots for different categories (V, C and S in the dummy data) for values of different variables (v1-v5) to see how ranges of the different categories vary across the variable. Is there a way to do it in R ?
Factor v1 v2 v3 v4 v5
v 12.0 8.4 5.3 3.0 3.5
v 11.8 7.5 4.7 2.8 3.7
c 13.2 9.5 5.7 3.0 4.1
c 13.4 9.3 6.3 3.3 3.8
c 10.5 7.7 5.0 2.5 3.2
s 13.1 9.6 5.4 3.1 3.9
s 15.0 9.0 5.1 4.5 9.0
Its should be a very simple code using some package, but I have not been able to figure it out.
Upvotes: 0
Views: 134
Reputation: 83215
You could do:
library(data.table)
df2 <- melt(setDT(df), measure.vars = patterns("^v"))
library(ggplot2)
ggplot(df2, aes(x=Factor, y=value1)) +
stat_summary(aes(fill=variable), fun.y="sum", geom = "bar", position="dodge") +
theme_bw()
this gives:
Used data:
df <- read.table(text="Factor v1 v2 v3 v4 v5
v 12.0 8.4 5.3 3.0 3.5
v 11.8 7.5 4.7 2.8 3.7
c 13.2 9.5 5.7 3.0 4.1
c 13.4 9.3 6.3 3.3 3.8
c 10.5 7.7 5.0 2.5 3.2
s 13.1 9.6 5.4 3.1 3.9
s 15.0 9.0 5.1 4.5 9.0", header=TRUE)
Upvotes: 3
Reputation: 56
there is another way to achieve this task
df<- read.csv("dummy.csv",header=TRUE,sep=",")
df
class(df)
df2<-aggregate(cbind(df$v1,df$v2,df$v3,df$v4,df$v5)~df$Factor, FUN=sum)
df2
barp<-barplot(t(df2[ , -1]),col=c("blue", "red", "green", "orange", "gold"));
axis(side = 1, at = barp, labels = df2$`df$Factor`)
Upvotes: 0