在plotly R中包装ggplot2时,图例中的额外变量

时间:2017-07-24 18:03:13

标签: r ggplot2 legend plotly aesthetics

我无法将ggplot包含在ggplotly()中的以下shape的图例展示为只有一种美学。它目前为每个图例条目显示三个变量(colorlinetypeaes()),但我只想显示一个。

此图中只有一个linetype值发生了变化(aes()),但其他值对应于特定变量,并且应该在我网站上的许多图表中保持一致。在我的情况下,简单地从绘图中删除其他tooltip值并不是一个可行的解决方案,因为我希望它们在其他类似的图中进行更改。此外,隐藏图例并修改library(ggplot2) library(plotly) #aes lists solute_colors <- c("NO3" = "#BF1616") source_shapes <- c("rain"= 21) location_linetypes <- c("1"= 2,"2"= 1,"3"= 3) #create dataframe data <- data.frame( date = c(1966, 1970, 1972, 1979, 1989, 1990, 1998, 2000), concentration = sample(1:8), solute = c("NO3", "NO3", "NO3", "NO3", "NO3", "NO3", "NO3", "NO3"), location = c("3", "1", "2", "3", "2", "1", "1", "2"), source = c("rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain") ) #ggplot ggplot(data, aes(x= date, y= concentration, linetype= location, color= solute, shape= source))+ geom_point() + geom_line() + scale_shape_manual(values = source_shapes) + scale_color_manual(values = solute_colors)+ guides(shape = F, color = F)+ #removes shape and source legends in ggplot, but not in ggplotly scale_linetype_manual(values = location_linetypes) 以显示信息,但不是最终结果。

运行时,以下代码:

linetype

图例仅显示ggplotly,这是期望的结果(见here)。但是,将其包装在#ggplot p p<-ggplot(data, aes(x= date, y= concentration, linetype= location, color= solute, shape= source))+ geom_point() + geom_line() + scale_shape_manual(values = source_shapes) + scale_color_manual(values = solute_colors)+ guides(shape = F, color = F)+ #removes shape and source legends in ggplot, but not in ggplotly scale_linetype_manual(values = location_linetypes) #wrap p in ggplotly ggplotly(p)

中时
aes()

图例显示的是图例中相同行中的三个ggplotly

here

如果在ggplot中换行或在图例中为其手动编码,如何覆盖此更改?我在ggplot中添加了主题,可以更改ggplotlylegend.position中的图例(例如legend.title2013-01-01 04:23:56-05:00)但我还没有找到任何可以控制实际的内容变量显示。

我在Windows 10上使用R版本3.4.0(RStudio版本1.0.143)。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:4)

不知道如何强制ggplotly尊重您的图例标签,但您可以手动覆盖几乎所有内容。

gp <- ggplotly(p)
gp[['x']][['data']][[1]][['name']] <- '1'
gp[['x']][['data']][[2]][['name']] <- '2'
gp[['x']][['data']][[3]][['name']] <- '3'
gp

enter image description here