我正在嘗試使用 ggplot2 使用資料幀 (df) 中的值制作某種時間線。我已經設法完全按照我想要的方式繪制資料(按此確切順序連接 x 標記的不同顏色的線段,即從左到右:“早”、“未知”、“晚”、“子” ')。資料框中的startpoint和endpoint列用于定義點和線段的位置。
問題是圖例沒有顯示“x”圖示的顏色,它們只是灰色。我試過添加scale_color_manual()和scale_fill_manual()命令,但它們似乎沒有改變任何東西。當我將形狀更改為 時,圖例確實顯示了正確的顏色shape = 21,但是,我真的希望形狀為4(x 圖示)。我不關心圖例的形狀,但scale_shape_manual()同樣沒有改變圖例的任何內容。我還試圖將不同color引數內側和外側aes()的引數ggplot(),geom_segment()和/或geom_point()。
如何使圖例中的圖示顯示正確的顏色?
下面我添加了一段代碼來重現問題。
library(ggplot2)
library(RColorBrewer)
## Define dataframe
df <- data.frame(Var = c("sub","late","unknown","early"),
Time = c(10,267,0,1256),
Endpoint = c(1533,1523,1256,1256),
Startpoint = c(1523,1256,1256,0))
colorscheme <- RColorBrewer::brewer.pal(9, "Set1")[c(1,4,2,3)]
## Make plot
ggplot(df, aes(x="", y=Endpoint, fill=Var), color =colorscheme)
geom_segment( aes(x="", xend="", y=Startpoint, yend=Endpoint), color = colorscheme)
geom_point(aes(x="", y=Endpoint),size=5, shape=4 , color = colorscheme)
coord_flip()

在此先感謝您的任何建議!
uj5u.com熱心網友回復:
您應該使用color而不是fill. 要從圖例中洗掉該行,請使用guides(color = guide_legend(override.aes = list(linetype = 0)))或使用show.legend = Fin geom_segment。
此外,傳入的引數之后ggplot不需要重復。
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme)
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint))
geom_point(size=5, shape=4)
coord_flip()
guides(color = guide_legend(override.aes = list(linetype = 0)))
#or
ggplot(df, aes(x="", y=Endpoint, color=Var), colorscheme)
geom_segment(aes(xend="", y=Startpoint, yend=Endpoint))
geom_point(size=5, shape=4)
coord_flip()

uj5u.com熱心網友回復:
嘗試這個:
ggplot(df, aes(x = "", y = Endpoint, color = Var), colorscheme)
geom_segment(aes(x = "", xend = "", y = Startpoint, yend = Endpoint), show.legend = FALSE)
geom_point(aes(x = "", y = Endpoint), size = 5, shape = 4)
coord_flip()
這樣圖例將只顯示 X
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371921.html
