在一个图中组合 geom_point 和 geom_line
combining geom_point and geom_line in one plot
我有两个类似于这些的数据框:
date <- c("2014-07-06", "2014-07-06","2014-07-06","2014-07-07", "2014-07-07","2014-07-07","2014-07-08","2014-07-08","2014-07-08")
TIME <- c("01:01:01", "10:02:02", "18:03:03","01:01:01", "10:02:02", "18:03:03","01:01:01", "10:02:02", "18:03:03")
depth <- c(12, 23, 4, 15, 22, 34, 22, 12, 5)
temp <- c(14, 10, 16, 13, 10, 9, 10, 14, 16)
depth.temp <- data.frame(date, TIME, depth, temp)
depth.temp$asDate<-as.Date(depth.temp$date)
date <- c("2014-07-06", "2014-07-07","2014-07-08")
meandepth <- c(13, 16, 9)
cv <- c(25, 9, 20)
depth.cv <- data.frame(date, meandepth, cv)
depth.cv$asDate<-as.Date(depth.cv$date)
从我创建的第一个情节开始:
library(ggplot2)
p1 <- qplot(asDate, depth, data=depth.temp, colour=temp, size = I(5), alpha = I(0.3))+ scale_y_reverse()
p1 + scale_colour_gradientn(colours = rev(rainbow(12)))
从第二个开始这个情节:
p2 <- ggplot(depth.cv, aes(x=asDate, y=meandepth))+ scale_y_reverse()
p2 + geom_line(aes(size = cv))
我想把两个图合二为一,点在后面,线在前面,有什么建议吗?请注意,点和线不是来自相同的数据,而是来自两个不同的数据框。
您可以将 data
添加到任何 geom_
,而不管您在主 ggplot
调用中使用什么。为此,我将跳过主要 ggplot
调用中的任何数据或美学映射分配,并在各自的 geom_
:
中完成它们
library(scales)
gg <- ggplot()
gg <- gg + geom_point(data=depth.temp, aes(x=asDate, y=depth, color=temp), size=5, alpha=0.3)
gg <- gg + geom_line(data=depth.cv, aes(x=asDate, y=meandepth, size=cv))
gg <- gg + scale_color_gradientn(colours=rev(rainbow(12)))
gg <- gg + scale_x_date(labels=date_format("%Y-%m-%d"))
gg <- gg + scale_y_reverse()
gg <- gg + labs(x=NULL, y="Depth")
gg <- gg + theme_bw()
gg
在我的例子中,为了显示线图,我必须将 aes 设置为:
aes(x=asDate, y=meandepth, group=1)
在geom_line情节中
我有两个类似于这些的数据框:
date <- c("2014-07-06", "2014-07-06","2014-07-06","2014-07-07", "2014-07-07","2014-07-07","2014-07-08","2014-07-08","2014-07-08")
TIME <- c("01:01:01", "10:02:02", "18:03:03","01:01:01", "10:02:02", "18:03:03","01:01:01", "10:02:02", "18:03:03")
depth <- c(12, 23, 4, 15, 22, 34, 22, 12, 5)
temp <- c(14, 10, 16, 13, 10, 9, 10, 14, 16)
depth.temp <- data.frame(date, TIME, depth, temp)
depth.temp$asDate<-as.Date(depth.temp$date)
date <- c("2014-07-06", "2014-07-07","2014-07-08")
meandepth <- c(13, 16, 9)
cv <- c(25, 9, 20)
depth.cv <- data.frame(date, meandepth, cv)
depth.cv$asDate<-as.Date(depth.cv$date)
从我创建的第一个情节开始:
library(ggplot2)
p1 <- qplot(asDate, depth, data=depth.temp, colour=temp, size = I(5), alpha = I(0.3))+ scale_y_reverse()
p1 + scale_colour_gradientn(colours = rev(rainbow(12)))
从第二个开始这个情节:
p2 <- ggplot(depth.cv, aes(x=asDate, y=meandepth))+ scale_y_reverse()
p2 + geom_line(aes(size = cv))
我想把两个图合二为一,点在后面,线在前面,有什么建议吗?请注意,点和线不是来自相同的数据,而是来自两个不同的数据框。
您可以将 data
添加到任何 geom_
,而不管您在主 ggplot
调用中使用什么。为此,我将跳过主要 ggplot
调用中的任何数据或美学映射分配,并在各自的 geom_
:
library(scales)
gg <- ggplot()
gg <- gg + geom_point(data=depth.temp, aes(x=asDate, y=depth, color=temp), size=5, alpha=0.3)
gg <- gg + geom_line(data=depth.cv, aes(x=asDate, y=meandepth, size=cv))
gg <- gg + scale_color_gradientn(colours=rev(rainbow(12)))
gg <- gg + scale_x_date(labels=date_format("%Y-%m-%d"))
gg <- gg + scale_y_reverse()
gg <- gg + labs(x=NULL, y="Depth")
gg <- gg + theme_bw()
gg
在我的例子中,为了显示线图,我必须将 aes 设置为:
aes(x=asDate, y=meandepth, group=1)
在geom_line情节中