0

How I can overlay the scatter plot of AverageTime2 vs. AverageCost and AverageTime1 vs. AverageCost. (in ggplot)?

Type<-c("a","b","c","d","e","f","g","h","i","j","k")
AverageTime<-c(12,14,66,123,14,33,44,55,55,6,66)
AverageTime2<-c(14,15,66,126,14,100,144,55,55,16,90)
AverageCost<-c(100,10000,400,20000,500000,5000,700,800,400000,500,120000)
AverageCost2<-c(10000,10000,4000,20000,5000,50000,7000,8000,40000,50000,120000)
df<-data.frame(Type,AverageTime,AverageTime2,AverageCost,AverageCost2)
Prradep
  • 4,719
  • 3
  • 34
  • 66
shoorideh
  • 173
  • 4
  • 16
  • Melt your data into long format. Anyone have a good r-faq dupe for this? – Gregor Thomas Jan 26 '16 at 20:39
  • Possible dupe, though it's a more complicated case: http://stackoverflow.com/q/1313954/903061 – Gregor Thomas Jan 26 '16 at 20:40
  • 1
    Two `geom_point` layers. `ggplot(data = df) + geom_point(aes(AverageCost, AverageTime), color = 'red') + geom_point(aes(AverageCost, AverageTime2), color = 'blue')` – alistaire Jan 26 '16 at 20:55
  • @alistaire, Thanks for your suggestion. How can I have geom_smooth() curve? Also How can I show that the color refers to which variable? – shoorideh Jan 26 '16 at 21:20
  • `+ geom_smooth( ... )`. Legends are more finicky with this approach; it may be easier to reshape your data as the answers below suggest. – alistaire Jan 26 '16 at 21:32

2 Answers2

1

You can simply build your data like this

df<-rbind(data.frame(Type,AverageTime,AT="T1",AverageCost,AverageCost2),
          data.frame(Type,AverageTime=AverageTime2,AT="T2",
                     AverageCost,AverageCost2))

and plot it that way

library(ggplot2)
ggplot(df)+geom_point(aes(AverageTime, AverageCost,color=AT))

enter image description here

HubertL
  • 17,371
  • 2
  • 20
  • 40
0

You'll need to reshape your data so that the AverageTime and AverageCost are each a column, with a separate column (called something like Type2) that distinguishes the two types.

You can accomplish this with dplyr, tidyr, and stringr:

library(dplyr)
library(tidyr)
library(stringr)

df_gather <- df %>%
  gather(column, value, -Type) %>%
  mutate(Type2 = str_detect(column, "2"),
         column = str_replace(column, "2", "")) %>%
  spread(column, value)

The particularly important part of that is to gather all the "Average" columns into one column, then to distinguish them (using the Type2 column), and then to spread them back into separate AverageCost and AverageTime columns.

Afterwards, you can simply do:

ggplot(df_gather, aes(AverageTime, AverageCost, color = Type2)) +
  geom_point()

enter image description here

David Robinson
  • 71,331
  • 13
  • 150
  • 174