簡體   English   中英

如何在ggplot中的一個圖中將兩個數據框中的兩個圖組合起來?

[英]How to combine two plots from two data frame in one plot in ggplot?

我有兩個具有相同變量的獨立數據集,並且已經創建了兩個單獨的圖。 我想將它們合並在一個圖中。

以下是每個情節的代碼:

##For tb2 :
my_data2$Date<- as.Date(my_data2$Date) #factor to date
tb2 <- my_data2
str(tb2)
tb2 <- tb2 %>% mutate (Month = month(Date, label = TRUE), Year = 
as.integer(year(Date)))

## plotting bar chart  
tb2_plot <- tb2 %>% ggplot(aes(factor(Month), GWQ)) + 
geom_bar(stat = "identity")+ facet_wrap(vars(Year), nrow = 1)+ 
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 9))

##For tb:
my_data$Date<- as.Date(my_data$Date) #factor to date
tb <- my_data
str(tb)
tb <- tb %>% mutate (Month = month(Date, label = TRUE), Year = 
as.integer(year(Date)))
## plotting bar chart   
tb_plot <- tb %>% ggplot(aes(factor(Month), GWQ))+ geom_bar(stat = 
 "identity")+ facet_wrap(vars(Year), nrow = 1)+ theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 9))

您需要在繪圖之前組合數據。

嘗試:

tb_all = bind_rows(tb %>% mutate(category = "A"), tb2 %>% mutate(category = "B"))

如果要區分繪圖中的兩組數據,可以將fill = category添加到aes()調用中。

暫無
暫無

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

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