【问题标题】:plot line graph two columns on x axis in r在r中的x轴上绘制线图两列
【发布时间】:2016-01-11 03:50:15
【问题描述】:

我在 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 %),在 y 轴上显示 col1 (member_id) 的计数。

样本数据:

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)

提前致谢,

尼尔

【问题讨论】:

  • 我认为您需要显示一些数据以及您想要的结果
  • @Batanichek :我已经分享了样本数据。请帮忙!

标签: r data-visualization


【解决方案1】:

如果我理解正确,你需要类似的东西

我显示在我的简单数据上

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'))

输出

【讨论】:

  • 感谢您的方法。我理解了这个概念。如果我需要使用 ggplot2 绘制相同的图,我们可以这样做吗?
  • 在ggplot中添加怎么做
  • @Batanichek 感谢您的支持!如何接受你的回答!!
  • @ Batanichek 我已接受您的回答。我还需要一点帮助,我正在努力实现您在 ggplot2 中的 x 轴标签和图例的基本图中所做的事情。请指导我如何在 ggplot2 中像 0a 100b 、 100a 0b 一样做同样的事情
  • 编辑 ggplot ,但可能有更好的方法来做到这一点
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多