[英]R ggpairs change the colour of bar histograms on diagonal
我想更改成對圖上直方圖條形的顏色,從而為每個變量設置不同的顏色。 看來我可以通過將 'fill =' 選項更改為新顏色來更改所有對角直方圖的顏色,但是當我嘗試替換四種顏色的列表時,我收到錯誤報告:
“r:美學必須是長度為1或與數據相同(15):填充”
因此,顏色似乎以某種方式與為直方圖指定的 bin 數量而不是每個直方圖的填充顏色相關聯?
為對角線上的每個直方圖實現不同顏色的最佳方法是什么?
示例代碼如下:
require(GGally)
# function for scatter plots with smoothed trend line
lower_plots <- function(data, mapping, ...) {
ggplot(data = data, mapping = mapping) +
geom_point(color = "black", shape = 1, size = 1, alpha = 1) +
geom_smooth(method = "gam",...)
}
# colour palette for histograms - subsitue for fill = ?
clrs <- c("red","green","blue","orange")
# pairs plot
ggpairs(iris,1:4,
diag = list(continuous = wrap("barDiag", bins = 15, fill = "blue")),
lower = list(continuous = wrap(lower_plots, color="red", se=F))) +
theme_light(base_size = 12) +
theme_light(base_size = 12) +
scale_fill_manual(values = ZNS[order(ZNS$Zone), 4]) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(plot.margin = unit(c(2.5,2.5,2.5,3.5), "lines")
)
我通過在函數內部使用全局賦值找到了解決您問題的方法。 首先,我認為這是不可能的,因為缺乏能夠在輸入的一列中識別如何為直方圖着色,因為每行都包含多個觀察結果......我測試了一下並想知道,考慮到一切的事實分批/循環處理,全局分配會有所幫助 - 它確實:
require(GGally)
# function for scatter plots with smoothed trend line
lower_plots <- function(data, mapping, ...) {
ggplot(data = data, mapping = mapping) +
geom_point(color = "black", shape = 1, size = 1, alpha = 1) +
geom_smooth(method = "gam",...)
}
# working with a trick of global assignment
diag_plots <- function(data, mapping, ...) {
# increase counter each run globally so outside the function as well and this does the trick!
x <<- x + 1
ggplot(data = data, mapping = mapping) +
# choose color by counter and send bin width argument in
geom_histogram(fill = clrs[x], ...)
}
# set the color and counter
clrs <- c("red","green","blue","orange")
x <- 0
# pairs plot
ggpairs(iris,1:4,
diag = list(continuous = wrap(diag_plots, bins = 15)),
lower = list(continuous = wrap(lower_plots, color="red", se=FALSE))) +
theme_light(base_size = 12) +
theme_light(base_size = 12) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(plot.margin = unit(c(2.5,2.5,2.5,3.5), "lines"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.