[英]create boxplots with transparent colour ggplot2
我創建了一個包含多個組的圖形,並在一行線上繪制了一個geom_boxplot()。 但是,透明地對盒子進行着色以便可以看到線條會很不錯。
這是一些示例數據:
x11()
name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
year <- c("2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)
df <- data.frame(name, class, year, value)
df
我繪制圖形:
p1 <- ggplot(df, aes(year, value))
p1 <- p1 + geom_line(aes(group=name, size=name),colour="#ff2300",alpha=0.5) + facet_wrap(~ class, scales = "free_y")
p1 <- p1 + geom_boxplot(aes(group=name))
print(p1)
在我的系統上,圖例中的線寬未正確顯示。 難道我做錯了什么? 提前致謝!
您可以將alpha
參數添加到箱線圖中。 例如:
geom_boxplot(aes(group=name), alpha = 0.8)
會給你
更改geoms的順序,先繪制箱線圖,然后再繪制線條。 但是,我不認為你的圖表是有道理的。 為什么使用名稱來改變線的大小? 改變線型不是更有意義嗎? 我建議不要使用free_y,因為它很難比較。
name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
year <- c("2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)
df <- data.frame(name, class, year, value)
df
library(ggplot2)
p1 <- ggplot(df, aes(year, value))
p1 <- p1 + geom_boxplot(aes(group=name)) + geom_line(aes(group=name, size=name),colour="#ff2300",alpha=0.5) +
facet_wrap(~ class, scales = "free_y")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.