簡體   English   中英

從 ggplot2 中的小圖例框中刪除邊框和顏色

[英]Removing borders and color from small legend boxes in ggplot2

我正在嘗試消除圖例鍵中小圖例框周圍的邊框和顏色,但似乎無法使其正常工作。 我已經嘗試了各種方法來解決我在這里發現的密切相關的問題,但到目前為止還沒有解決。 我只想將線型留在圖例中,但擺脫框和彩色邊框。 這是我的示例數據和 r 代碼。

y = c( 0.06224745, 0.3580695, 0.3200822, 0.2530397, 0.169859, 0, 0.3620131, 0.1681453, 0.3327576, 0.05393907, 0.2320595, 0.4062182, 0.1692738, 0.2514794, 0.1024528, 0.2209554, 0.763748, 0.03592918, 0.2773448, 1.081272, 0.006706128, 0.2940041, 0.487123, 0.0001013974, 0.8474042)
x = c( 32, 41, 38, 49, 28, 6, 77, 60, 48, 21, 61, 67, 45, 89, 29, 53, 86, 19, 47, 68, 15, 34, 91, 3, 105)
mp1 = c( 0.2973326, 0.3132524, 0.3243703, 0.2555882, 0.2339925, 0.0006379165, 0.1849138, 0.1964182, 0.262851, 0.07561027, 0.1932121, 0.1816839, 0.2857122, 0.2248831, 0.2534116, 0.2293056, 0.2088319, 0.04291189, 0.2703401, 0.1808885, 0.009066496, 0.31503, 0.239224, 0.001911957, 0.5340882)
mp2 = c( 0.2091168, 0.2556768, 0.2498977, 0.2723394, 0.1528573, 0.0002013313, 0.6549563, 0.3847091, 0.2677775, 0.0369603, 0.4001009, 0.5029076, 0.25977, 0.6684505, 0.169202, 0.3002027, 0.6739084, 0.01807663, 0.2642043, 0.5208559, 0.002514641, 0.2282424, 0.6667941, 0.002242293, 0.8841164)
l1 = c( 0.2585209, 0.2804518, 0.2904638, 0.2290389, 0.2039077, 0.000337117, 0.1646497, 0.1740522, 0.2345254, 0.06316437, 
0.1703116, 0.1600252, 0.2538304, 0.2003988, 0.2192846, 0.207571, 0.1860312, 0.03582043, 0.2407754, 0.1599377, 0.007155506, 0.2736827, 0.2129463, 0.001105815, 0.4845784) 
l2 = c( 0.1733961, 0.2250107, 0.2129352, 0.2365371, 0.1294626, 7.192024E-5, 0.5868036, 0.3324016, 0.2333063, 0.02924621, 0.3449358, 0.4388901, 0.2255638, 0.5995129, 0.1417819, 0.2647597, 0.6046445, 0.014307, 0.2299643, 0.4563084, 0.001781163, 0.1894632, 0.5990405, 0.0009575819, 0.7855351) 
u1 = c( 0.339663, 0.3522312, 0.3688239, 0.2869844, 0.2624688, 0.001020211, 0.208165, 0.2178297, 0.2950362, 0.08898896, 0.2164218, 0.2028822, 0.3185223, 0.2513164, 0.286148, 0.254538, 0.2336157, 0.05095191, 0.3031861, 0.2008639, 0.01083725, 0.3591531, 0.2675789, 0.002868453, 0.5985374) 
u2 = c( 0.2447536, 0.2879884, 0.2796768, 0.3201898, 0.179591, 0.0003747398, 0.7450722, 0.4500551, 0.316286, 0.04545882, 0.471318, 0.5739883, 0.3043829, 0.7392681, 0.1991603, 0.3434644, 
0.7537116, 0.02280851, 0.3125538, 0.5912406, 0.003344284, 0.264536, 0.7306693, 0.003902742, 0.9911295)

my_df <- data.frame(x, y, mp1, mp2, l1, l2, u1, u2)

ggplot(my_df, aes(x = x, y = y))+
    geom_point(aes(color="Daily values"))+
    geom_line(aes( x = x, y = mp2, color = "High risk area"),size=1)+
    geom_line(aes(x = x, y = mp1, color = "Low risk area"),size=1)+
    geom_ribbon(data = my_df, aes(ymin=l2, ymax=u2, color="95% CI"), alpha=0.3)+
    geom_ribbon(data = my_df, aes(ymin=l1, ymax=u1, color="95% CI"), alpha=0.3)+
    scale_color_manual(values=c("gray60","darkblue","darkgreen","black"))+
    ylab("My outcome")+
    guides( color = guide_legend(title="My Pred Trend",
                                 override.aes = list(linetype = c(0,1,1,1)),
                                 shape=c(1,NA,NA,NA),
                                 reverse=T))+
    ggtitle("My Trend for this Area ")+
    theme(legend.key = element_rect(fill = "white"))

我嘗試的一個建議是按照此處的建議在geom_ribbon中采用aes()之外的顏色。

這種方法似乎很有效,忽略了圖例中的置信區間。 但是,在第二步中,當我嘗試按照上面鏈接中的建議手動添加置信區間的圖例時,它不起作用。

我還嘗試在我最后一行代碼中提供的方法,但這似乎並沒有改變任何東西。

我認為這個問題可能與geom_ribbon()以及 colors scale_color_manual()的手動規范有關

他們是一種擺脫彩色邊框並在保留linetype()的同時使圖例清晰的方法嗎?

這是示例 plot:

樣本圖

您可以使用show.legend = FALSE在您希望一個 geom-legend 不出現的地方。

library(ggplot2)


ggplot(my_df, aes(x = x, y = y))+
  geom_point(aes(color="Daily values"))+
  geom_line(aes( x = x, y = mp2, color = "High risk area"),size=1, show.legend = T)+
  geom_line(aes(x = x, y = mp1, color = "Low risk area"),size=1, show.legend = F)+
  geom_ribbon(data = my_df, aes(ymin=l2, ymax=u2, color="95% CI"), alpha=0.3, show.legend = F)+
  geom_ribbon(data = my_df, aes(ymin=l1, ymax=u1, color="95% CI"), alpha=0.3, show.legend = F)+
  scale_color_manual(values=c("gray60","darkblue","darkgreen","black"))+
  ylab("My outcome")+
  guides( color = guide_legend(title="My Pred Trend",
                               override.aes = list(linetype = c(0,1,1,1)),
                               shape=c(1,NA,NA,NA),
                               reverse=T))+
  ggtitle("My Trend for this Area ")+
  theme(legend.key = element_rect(fill = "white"))

暫無
暫無

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

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