我是否可以改變切面的某些值的背景?
我在faceting plot中擁有50個情節。我想知道我是否可以通過改變所選情節的背景來突出其中的一些情節。
具體來說,下面的圖片是我的ggplot結果吧。我想通過把它們的背景染成淺藍色來突出37& 42。
我想過使用ifelse。但是我不確定如何定義控制流。

下面是我的代碼
axr_treat_ctrl %>%
ggplot(aes(x= year, y= mtopic。 colour=Type, group=Type)) >
geom_line( )
geom_point( size= 1)
scale_y_continuous(breaks= extended_breaks(n = 5) )
scale_x_continuous(breaks = extended_breaks(n = 10) )
geom_vline(xintercept = 2005。 linetype=2。 color= "brown")
geom_vline(xintercept = c(2010。 2011。 2017),linetype=3。 color= "blue")
labs(x= "x-axis",)
y ="y-axis")
facet_wrap(~topicr )
指導(x = guide_axis(angle = 90))
主題(legend.position = "bottom"/span>)
# ifelse ( topicr==37, call( expr(panel.background = element_rect(fill = "lightblue")),
# call( expr( panel.background = element_rect(fill = "white") )) )
uj5u.com熱心網友回復:
我認為最好的方法是直接操作你的資料框架,axr_treat_ctrl。這里有一個簡單的、單獨的例子,因為你的例子是不可復制的。
library(ggplot2)
mtcars <- data.frame(mtcars)
mtcars$mazda < - ifelse(grepl("Mazda", rownames(mtcars))。 1,0)
ggplot(mtcars,aes(x=wt。 y=mpg))
geom_rect(aes(fill=mazda)/span>。 xmin = -Inf。 xmax = Inf,xmax
ymin = -Inf。 ymax = Inf,/span>alpha=0. 2)
geom_point()
facet_wrap(~rownames(mtcars))
主題(legend.position="none"/span>)
在這個例子中,我想為每輛汽車的構建有一個單獨的情節,然后為有馬自達構建的汽車的背景著色。因此,我添加了一個簡單的二進制指標變數mazda,它決定了觀察是否是一輛馬自達汽車。然后我將mazda變數傳入geom_rect。而輸出結果是:
根據@camille的評論洗掉了圖例。你也可以通過fill=factor(mazda)來代替mazda來獲得一個離散的圖例,這取決于你所尋找的東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330217.html
標籤:
上一篇:在程式集中列印文本
下一篇:ggplot2中柱狀圖的密度值?

