在r中的x轴上绘制线图两列 [英] plot line graph two columns on x axis in r

查看:119
本文介绍了在r中的x轴上绘制线图两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在excel中有一个数据集。共有9列:

I have a data set in excel. There are total 9 columns:

col1-member_id

col1 - member_id

col2-A_timespent_in_hrs

col2 - A_timespent_in_hrs

col3-B_timespent_in_hrs

col3 - B_timespent_in_hrs

col4-total_timespent_in_hrs(col2 + col3)

col4 - total_timespent_in_hrs (col2 + col3)

col5- A_pv(问题中不考虑)

col5 - A_pv (not to be considered in the problem)

col6-B_pv(问题中不考虑)

col6 - B_pv (not to be considered in the problem)

col7-total_pv(col5 + col6)(在问题中不考虑)

col7 - total_pv (col5 + col6)(not to be considered in the problem)

col8-A_timespent_in_hrs%to total_timespent_in_hrs

col8 - A_timespent_in_hrs % wrt to total_timespent_in_hrs

col9-B__timespent_in_hrs%wrt to total_timespent_in_hrs

col9 - B__timespent_in_hrs % wrt to total_timespent_in_hrs

我需要在R(线形图)中绘制一个图,其中需要显示col8(A_timespent_in_hrs% )和col9(B_timespent_in_hrs%)在x轴上,而col1(member_id)的计数在y轴上。

I need to plot a graph in R (line graph) where i need to show the col8 ( A_timespent_in_hrs %) and col9 ( B_timespent_in_hrs %) on x axis and the count of col1 ( member_id) on the y axis.

样本数据:

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的计数。

Here i am trying to plot the percentage of col8 and col9 on x axis and count of col1 on y axis.

图形应为单线,例如col8从0,0坐标开始,值为100%,因此col9在该点将为0%,类似地,在x轴上的任何点
上col9为100%,因此col8在该点将为0%。

The graph should be of single line for example col8 starts from 0,0 cordinate with 100% value so col9 would be 0% at that point, similarly on any point on x axis col9 is 100% so the col8 would be 0% at that point.

在图的中间都是col8 a nd col9将显示col1计数的50%。

And at the middle of the graph both col8 and col9 would be showing the 50% of the count of col1.

注意:col8和col9总是在相加时提供100%,例如(0 + 100、1 + 99, 2 + 98、3 + 97)

Note: col8 and col9 always give 100% on adding like (0 + 100, 1 + 99, 2 + 98, 3 + 97)

预先感谢

Neel

推荐答案

如果我理解正确,则需要类似

If i understnd right you need something like

我在我的简单数据上显示

i show on my simple data

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)

1) make count for col8 and col9 ( i use 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)

I think main what you need its par(new=T)

对于ggplot您可以简单地

for ggplot you can simply

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

输出

这篇关于在r中的x轴上绘制线图两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆