如何覆盖AverageTime2与AverageCost和AverageTime1与AverageCost的散点图。 (在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)
答案 0 :(得分:1)
您可以像这样构建数据
df<-rbind(data.frame(Type,AverageTime,AT="T1",AverageCost,AverageCost2),
data.frame(Type,AverageTime=AverageTime2,AT="T2",
AverageCost,AverageCost2))
并以那种方式绘制
library(ggplot2)
ggplot(df)+geom_point(aes(AverageTime, AverageCost,color=AT))
答案 1 :(得分:0)
您需要重新整形数据,以便AverageTime和AverageCost各自成为一列,并使用一个单独的列(称为Type2
)来区分这两种类型。
您可以使用dplyr,tidyr和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)
其中最重要的部分是收集所有&#34;平均值&#34;列成一列,然后区分它们(使用Type2
列),然后将它们分散回单独的AverageCost和AverageTime列。
之后,您可以这样做:
ggplot(df_gather, aes(AverageTime, AverageCost, color = Type2)) +
geom_point()