0

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

2
  • I think you need to show some data and what you want as result Commented Oct 13, 2015 at 12:04
  • @Batanichek : I have shared the sample data. Kindly help! Commented Oct 13, 2015 at 13:09

1 Answer 1

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

enter image description here

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 enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the approach. I understood the concept. If i need to plot the same using ggplot2 , can we do the same?
add how to do in ggplot
@ Batanichek Thanks for the hep! How to accept your answer!!
@ Batanichek I have accepted your answer. I need one more favour, I am trying to achieve what you have done in the basic plot the x axis labelling and legend, in ggplot2 . Kindly guide me how to do the same like 0a 100b , 100a 0b in ggplot2
Edit ggplot , but may be there is better way to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.