[英]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.