簡體   English   中英

在同一y軸上繪制兩條線; Ggplot,R

[英]Plot two lines on the same y-axis; Ggplot, R

我有一個ggplot圖,我想在上面畫兩條線(來自不同的列,但日期相同)。 我得到的是兩條彼此堆疊的線,但是我希望具有正確排列的y軸,並且這些線彼此重疊。

這是我要繪制的數據:

final_table:

 Month              a                      b
1 2018-04      758519.397875       2404429.258675
2 2018-05      964792.603725        1995902.14473
3 2018-06      703170.240575        1294997.84319

這是我的代碼:

bla3 <- melt(final_table, id='Month')
ggplot(data=bla3, aes(x=Month, y=value, colour= variable, group=variable)) + 
  geom_line() 

我得到的輸出(注意y軸是完全錯誤且無序的)。

gg_plot

我猜您的數據變量格式不正確。 例如,如果您跑步

class(final_table$month) 

這應該產生日期。 因此,您需要將其轉換為正確的格式。 這是您的電話號碼示例。

Month <- as.character(c("2018-04", "2018-05", "2018-06")) #or convert it to character after
a <- c(758519.397875, 964792.603725, 703170.240575)
b <- c(2404429.258675, 1995902.14473, 1294997.84319)

final_table <- data.frame(Month, a, b)

 #your Month variable is messed up, you actually need the day!
 final_table$Month <- as.Date(paste(final_table$Month,"-01",sep=""))

library(reshape) #need to load that for melt
bla3 <- melt(final_table, id='Month')
ggplot(data=bla3, aes(x=Month, y=value, colour= variable, group=variable)) +   
geom_line()

你去!

暫無
暫無

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

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