我有一个包含多个变量的数据框,我想知道如何将它们绘制成Excel
中的绘图选项。
只是一个简单的数据示例:
> V1 V2 V3
1 A 0
1 A 0
1 B 1
1 B 0
1 A 1
2 A 0
2 B 0
2 A 0
2 A 0
2 A 0
我希望拥有的是x
轴V1
,y
轴,V3
时V2
的所有数量均为A
B
或barplot
。
有人可以就如何做到这一点分享一些想法吗? 2*2
函数似乎不具备功能,因为它只能用于y
表?
谢谢。
编辑:
将V3
轴视为x
的百分比,V1
的{{1}}轴以及V2
的每个级别创建条形图。
答案 0 :(得分:3)
library( 'ggplot2' )
library( 'reshape2' )
df1 <- dcast( data = df1, formula = V1 ~ V2, value.var = 'V3', fun.aggregate = sum ) # get sum of V3 by grouping V1 and V2
df1 <- melt( data = df1, id.vars = 'V1') # melt data
df1
# V1 variable value
# 1 1 A 1
# 2 2 A 5
# 3 1 B 1
# 4 2 B 0
ggplot(data = df1, aes( x = factor( V1 ), y = value, fill = variable ) ) + # print bar chart
geom_bar( stat = 'identity' )
使用position = 'dodge
ggplot(data = df1, aes( x = factor( V1 ), y = value, fill = variable ) ) + # print bar chart
geom_bar( stat = 'identity', position = 'dodge' )
数据:强>
df1 <- read.table(text = 'V1 V2 V3
1 A 0
1 A 0
1 B 1
1 B 0
1 A 1
2 A 0
2 B 0
2 A 0
2 A 5
2 A 0', header = TRUE, stringsAsFactors = FALSE )
答案 1 :(得分:1)
首先,您需要获得包含要绘制的值的摘要dataframe
。
df <- data.frame(V1 = rep(1:2,each=5), V2 = c("A","A","B", "B", "A", "A", "B","A", "A", "A"),
V3 = c(0,0,1,0,1,0,0,0,0,0))
values <- aggregate(df$V3, list(V1 = df$V1, V2 = df$V2), sum)
# V1 V2 V3
# 1 1 A 1
# 2 2 A 0
# 3 1 B 1
# 4 2 B 0
ggplot(values, aes(x = factor(V1), y = V3, fill = V2))+
geom_bar(stat = "identity", width = 0.2)
或者,如果您不希望它们堆叠在彼此之上。添加一些标签。
ggplot(values, aes(x = factor(V1), y = V3, fill = V2))+
geom_bar(stat = "identity", width = 0.2, position = "dodge") +
labs(list(x = "x", y = "count",fill = "group"))
修改强>
我尝试直接在ggplot
上使用dataframe
而未做摘要,结果相同。
## a little change in V3
df <- data.frame(V1 = rep(1:2,each=5),
V2 = c("A","A","B", "B", "A", "A", "B","A", "A", "A"),
V3 = c(2,0,1,2,1,3,3,8,1,0))
## plot df directly
ggplot(df, aes(factor(V1), V3, fill = V2)) +
geom_bar(stat = "identity", width = 0.2, position = "dodge") +
labs(list(x = "x", y = "count",fill = "group"))