在 r 的 x 轴上绘制两列线图
plot line graph two columns on x axis in r
我在excel中有一个数据集。共有 9 列:
col1 - member_id
col2 - A_timespent_in_hrs
col3 - B_timespent_in_hrs
col4 - total_timespent_in_hrs (col2 + col3)
col5 - A_pv(不在题中考虑)
col6 - B_pv(不考虑问题)
col7 - total_pv (col5 + col6)(不在题中考虑)
col8 - A_timespent_in_hrs % wrt to total_timespent_in_hrs
col9 - B__timespent_in_hrs % wrt to total_timespent_in_hrs
我需要在 R 中绘制一个图表(折线图),其中我需要在 x 轴上显示 col8 ( A_timespent_in_hrs %) 和 col9 ( B_timespent_in_hrs %) 以及 col1 的计数( member_id) 在 y 轴上。
示例数据:
col1 col2 col3 col4 col5 col6 col7 col8 col9
6834682 0 534.27 534.27 0 2387 2387 0% 100%
46940 591.69 0 591.69 9508 0 9508 100% 0%
4903634 24.66 0 24.66 625 0 625 100% 0%
6777856 35.36 0 35.36 623 0 623 100% 0%
6327644 15.38 0 15.38 424 0 424 100% 0%
2581446 385.29 0 385.29 3743 0 3743 100% 0%
962509 158.6 0 158.6 3014 0 3014 100% 0%
6598387 0 87 87 0 304 304 0% 100%
6852254 0 301.04 301.04 0 1692 1692 0% 100%
这里我试图绘制 x 轴上 col8 和 col9 的百分比以及 y 轴上 col1 的计数。
图表应该是单线的,例如 col8 从 0,0 坐标开始,值为 100%,因此 col9 在该点为 0%,在任何点都类似
x 轴上的 col9 为 100%,因此此时 col8 为 0%。
在图表的中间,col8 和 col9 将显示 col1 计数的 50%。
注意:col8 和 col9 总是给 100% 添加像 (0 + 100, 1 + 99, 2 + 98, 3 + 97)
提前致谢,
尼尔
如果我没看错你需要类似
的东西
我在我的简单数据上展示
data=data.frame( a=c(10,10,30,30,100),val=c(43,54,21,34,67))
data$b=100-data$a
1) 计算 col8 和 col9(我使用 dplyr)
data1=group_by(data,a,b)
data1=summarize(data1,cnt=n())
2) 剧情
par(xpd=T)
plot(data1$a,data1$cnt,xlim=c(0,100),type="l",col="red",xaxt="n",xlab="")
text(cex=1, x=(0:10)*10, y=min(data1$cnt)-0.1, paste0((0:10)*10,"a"), xpd=TRUE, srt=90, pos=2)
par(new=T)
plot(data1$b,data1$cnt,xlim=c(0,100),type="l",col="green",xaxt="n",xlab="")
text(cex=1, x=(10:0)*10, y=min(data1$cnt)-0.25, paste0((0:10)*10,"b"), xpd=TRUE, srt=90, pos=2)
输出
我想主要是你需要什么par(new=T)
对于 ggplot 你可以简单地
ggplot(data1) +
geom_line(aes(y = cnt,x=a, colour = "green"),) +
geom_line(aes(y = cnt,x=b, colour = "red"))+
xlab("")+
theme_bw()+theme(legend.position = "none")+
scale_x_continuous(name="",
breaks = c(0,10, 20, 30, 40, 50,60,70,80,90,100),
labels = c('0a\n100b','10a\n90b', '20a\n80b', '30a\n70b', '40a\n60b', '50a\n50b', '60a\n40b'
, '70a\n30b'
, '80a\n20b' , '90a\n10b' , '100a\n0b'))
输出
我在excel中有一个数据集。共有 9 列:
col1 - member_id
col2 - A_timespent_in_hrs
col3 - B_timespent_in_hrs
col4 - total_timespent_in_hrs (col2 + col3)
col5 - A_pv(不在题中考虑)
col6 - B_pv(不考虑问题)
col7 - total_pv (col5 + col6)(不在题中考虑)
col8 - A_timespent_in_hrs % wrt to total_timespent_in_hrs
col9 - B__timespent_in_hrs % wrt to total_timespent_in_hrs
我需要在 R 中绘制一个图表(折线图),其中我需要在 x 轴上显示 col8 ( A_timespent_in_hrs %) 和 col9 ( B_timespent_in_hrs %) 以及 col1 的计数( member_id) 在 y 轴上。
示例数据:
col1 col2 col3 col4 col5 col6 col7 col8 col9
6834682 0 534.27 534.27 0 2387 2387 0% 100%
46940 591.69 0 591.69 9508 0 9508 100% 0%
4903634 24.66 0 24.66 625 0 625 100% 0%
6777856 35.36 0 35.36 623 0 623 100% 0%
6327644 15.38 0 15.38 424 0 424 100% 0%
2581446 385.29 0 385.29 3743 0 3743 100% 0%
962509 158.6 0 158.6 3014 0 3014 100% 0%
6598387 0 87 87 0 304 304 0% 100%
6852254 0 301.04 301.04 0 1692 1692 0% 100%
这里我试图绘制 x 轴上 col8 和 col9 的百分比以及 y 轴上 col1 的计数。
图表应该是单线的,例如 col8 从 0,0 坐标开始,值为 100%,因此 col9 在该点为 0%,在任何点都类似 x 轴上的 col9 为 100%,因此此时 col8 为 0%。
在图表的中间,col8 和 col9 将显示 col1 计数的 50%。
注意:col8 和 col9 总是给 100% 添加像 (0 + 100, 1 + 99, 2 + 98, 3 + 97)
提前致谢,
尼尔
如果我没看错你需要类似
的东西我在我的简单数据上展示
data=data.frame( a=c(10,10,30,30,100),val=c(43,54,21,34,67))
data$b=100-data$a
1) 计算 col8 和 col9(我使用 dplyr)
data1=group_by(data,a,b)
data1=summarize(data1,cnt=n())
2) 剧情
par(xpd=T)
plot(data1$a,data1$cnt,xlim=c(0,100),type="l",col="red",xaxt="n",xlab="")
text(cex=1, x=(0:10)*10, y=min(data1$cnt)-0.1, paste0((0:10)*10,"a"), xpd=TRUE, srt=90, pos=2)
par(new=T)
plot(data1$b,data1$cnt,xlim=c(0,100),type="l",col="green",xaxt="n",xlab="")
text(cex=1, x=(10:0)*10, y=min(data1$cnt)-0.25, paste0((0:10)*10,"b"), xpd=TRUE, srt=90, pos=2)
输出
我想主要是你需要什么par(new=T)
对于 ggplot 你可以简单地
ggplot(data1) +
geom_line(aes(y = cnt,x=a, colour = "green"),) +
geom_line(aes(y = cnt,x=b, colour = "red"))+
xlab("")+
theme_bw()+theme(legend.position = "none")+
scale_x_continuous(name="",
breaks = c(0,10, 20, 30, 40, 50,60,70,80,90,100),
labels = c('0a\n100b','10a\n90b', '20a\n80b', '30a\n70b', '40a\n60b', '50a\n50b', '60a\n40b'
, '70a\n30b'
, '80a\n20b' , '90a\n10b' , '100a\n0b'))
输出