簡體   English   中英

Plot 一個 x 軸上的多列,在 R 中,一列作為線 + 點,其他作為點圖

[英]Plot multiple columns in one x axis, as one as line + point and the others as point graphs in R

我有一個數據框,其中包含 6 個變量中的 90 個對象(一個是日期,另一個是 3 個月的車站溫度——這里添加了一部分)。 我想 plot 將站溫度數據與日期相對應,但station1 數據應該用點 + 線繪制,其他站僅在 points 中

df <- data.frame(Date= c(1:20),
                 Station1 = c(31.7,19.5,23.6,20.5,25.2,35.5,38.0,30.3,20.1,20.6,23.6,33.6,21.1,22.7,24.8,23.5,21.8,20.8,26.9,21.2),
                 Station2= c(10.3,12.2,13.3,13.4,13.4,14.5,25.1,22.7,16.0,15.8,13.0,16.0,16.9,16.4,17.2,15.8,15.6,16.7,16.8,16.9),
                 Station3 = c(26.4,15.8,18.0,15.6,22.6,30.4,31.7,26.5,18.2,19.9,23.2,28.0,16.7,20.1,21.4,19.4,20.1,19.8,25.0,20.3),
                 Station4 = c(8.6,8.8, 7.1,9.3,8.5,13.1,21.6,20.1,12.3,11.7,9.6,14.2,15.9,13.1,13.6,13.1,12.4,11.3,12.5,14.3),
                 Station5 = c(31.6,22.8,17.0,18.6,28.9,35.5,38.7,30.3,25.7,21.9,28.3,32.7,24.2,26.5,28.1,24.4,24.0,24.6,28.5,22.5))

我嘗試過不同的方式,

x <- df$Date
y1 <- df$Station1
y2 <- df$Station2
y3 <- df$Station3
y4 <- df$Station4
y5 <- df$Station5

g1 <- ggplot(df, aes(x)) +
  geom_line(aes(y=y1), color = "red")+
  geom_point(aes(y=y1), color = "red")+
  geom_point(aes(y=y2), color = "#00FF00") +
  geom_point(aes(y=y3), color = "blue") +
  geom_point(aes(y=y4), color = "#FF9933") +
  geom_point(aes(y=y5), color = "purple")

這並沒有給我一個傳奇,我也無法添加一個。

我嘗試使用 tidyr,

PD <- df %>%
  gather(type, Temperature, Station1, Station2, Station3, Station4, Station5)

ggplot(PD, aes(x = Date, y= Temperature, color = type)) + geom_point()

它創建點 plot,但不能為站一做點+線。

這也在嘗試,但由於日期成為行名而沒有奏效。

df1 <- data.frame(St1 = c(df$Station1),
                 St2 = c(df$Station2),
                 St3 = c(df$Station3),
                 St4 = c(df$Station4),
                 St5 = c(df$Station5),
                 row.names = c(c(df$DATE)))

q <- df1 %>%
  rownames_to_column() %>%
  gather(key = key, value = value, St1,St2) %>% 
  mutate(rowname = factor(rowname)) %>% 
  ggplot(aes(as.Date(rowname), value, color = key)) + 
  geom_point() + 
  geom_line() +
  labs(x="Dates", y="temperature", title ="Station temperatures")+
  theme_bw()

非常感謝任何幫助,因為我對 R 非常陌生。 提前致謝!

如果我說對了

library(tidyverse)
df1 <- df %>% pivot_longer(-Date)

ggplot(df1, aes(Date, value, color = name)) +
  geom_point() +
  geom_line(data = filter(df1, name == "Station1"), aes(Date, value, color = name))

你很親密。 您需要使用 ggplot scale_color_manual()將顏色關聯到每個站點。

這是您第一次嘗試的解決方案:

g1 <- ggplot(df, aes(x)) +
  geom_line(aes(y=y1, color = "y1"))+ # note that color = "y1" is within aes()
  geom_point(aes(y=y1, color = "y1"))+
  geom_point(aes(y=y2, , color = "y2")) +
  geom_point(aes(y=y3, color = "y3")) +
  geom_point(aes(y=y4, color = "y4")) +
  geom_point(aes(y=y5,  color = "y5")) +
  scale_color_manual(name = "Stations", # here is a custom scale that creates legend
                     values = c("y1" = "red",
                                "y2" = "#00FF00",
                                "y3" = "blue",
                                "y4" = "#FF9933",
                                "y5" = "purple"))
print(g1)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM