嗨,我有两个数据帧,其频率/百分比值如下。
Value Frequency Percentage
2 1 2.777778
3 7 19.444444
4 19 52.777778
5 9 25.000000
Value Frequency Percentage
1 2 3.773585
2 3 5.660377
3 8 15.094340
4 20 37.735849
5 20 37.735849
如您所见,这些值属于1-5的相同范围。但在某些情况下,某些值没有频率(df 1中缺少值1)。如何使用2 geom_lines在同一图表中绘制百分比值?我希望两条线的共同x轴从1-5开始。
答案 0 :(得分:0)
您可以对两个数据集进行行绑定,创建标记变量,以标识两个数据集,然后绘制
df1 = data.frame(Value = c(2,3,4,5),
Frequency = c(1,7,19,9),
Percentage = c(2.77,19.44,52.77,25))
df2 = data.frame(Value = c(1,2,3,4,5),
Frequency = c(2,3,8,20,20),
Percentage = c(3.77,5.66,15.09,37.73,37.73))
df1$Type = "A"
df2$Type = "B"
dfw = rbind(df1,df2)
ggplot(data = dfw, aes(x = Value, y = Percentage, color = Type)) + geom_line()
答案 1 :(得分:0)
df1<-data.frame(Value = c(2,3,4,5),
Frequency = c(1,7,19,9),
Percentage = c(2.777778,19.444444,52.777778,25.000000)
)
df2<-data.frame(Value = c(1,2,3,4,5),
Frequency = c(2,3,8,20,20),
Percentage = c(3.773585,5.660377,15.094340,37.735849,37.735849
))
df1$N<-factor(1)
df2$N<-factor(2)
df3<-rbind(df1,df2)
library(ggplot2)
plot<-ggplot(df3,aes(Value,Percentage, color = N))+
geom_line()
print(plot)