plot line graph two columns on x axis in r

老子叫甜甜 提交于 2019-12-11 23:07:28

问题


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

col1 - member_id

col2 - A_timespent_in_hrs

col3 - B_timespent_in_hrs

col4 - total_timespent_in_hrs (col2 + col3)

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

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

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

col8 - A_timespent_in_hrs % wrt to total_timespent_in_hrs

col9 - B__timespent_in_hrs % wrt to total_timespent_in_hrs

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.

Sample Data:

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%

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

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.

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

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

Thanks in advance,

Neel


回答1:


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

data1=group_by(data,a,b)
data1=summarize(data1,cnt=n())

2) plot

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)

output

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

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

output



来源:https://stackoverflow.com/questions/33101923/plot-line-graph-two-columns-on-x-axis-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!