【发布时间】:2017-12-30 10:17:06
【问题描述】:
我无法使以下 ggplot 的图例包含在 ggplotly() 中仅显示一种美学。它目前为每个图例条目显示三个变量(shape、color、linetype),但我只想显示一个。
此图中只有一个 aes() 值发生变化 (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,这是期望的结果(见here)。但是,将其包装在ggplotly
#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 或在图例中手动编码时如何覆盖此更改?我在 ggplot 中添加了主题,这些主题可以更改 ggplot 和 ggplotly 中的图例(例如 legend.position 和 legend.title),尽管我还没有找到任何东西来控制显示的实际变量。
我在 Windows 10 上使用 R 版本 3.4.0(RStudio 版本 1.0.143)。任何帮助将不胜感激!
【问题讨论】:
标签: r ggplot2 legend plotly aesthetics