[英]How to create multiple line chart in r
我在这个结构中有一个数据框
Date x1 x2 x3 x4
1/2/2018 500000 10000 10 80000
1/3/2018 600000 15000 13 70000
1/4/2018 300000 8000 7 40000
如何在同一个图表中使用 4 个 x 变量创建 ggplot 折线图,而且由于 x3 与其他值的关系很小,它可能会在图中丢失,是否有解决此问题的技巧?
谢谢。
首先,数据集。
df1 <- read.table(text = "
Date x1 x2 x3 x4
1/2/2018 500000 10000 10 80000
1/3/2018 600000 15000 13 70000
1/4/2018 300000 8000 7 40000
", header = TRUE)
df1$Date <- as.Date(df1$Date, "%m/%d/%Y")
下面将使用log10
比例绘制三条线。
library(ggplot2)
long <- reshape2::melt(df1, id.vars = "Date")
ggplot(long, aes(x = Date, y = value,
group = variable, colour = variable)) +
geom_line() +
scale_y_log10()
data <- airquality %>%
group_by(Month) %>%
summarise(mean_Ozone = mean(Ozone, na.rm=TRUE),
mean_Solar.R = mean(Solar.R, na.rm=TRUE),
mean_Wind = mean(Wind),
mean_Temp = mean(Temp))
data2 <- reshape2::melt(data, id.vars = "Month")
ggplot(data2, aes(x = Month, y = value,
group = variable, colour = variable)) +geom_line()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.