繁体   English   中英

ggplotly中组合的geom_bar和geom_point图例

[英]Combined geom_bar and geom_point legend in ggplotly

我正在尝试获取带有图例的组合的条形图+点图,用于不同的指标(指标)和点(指标的变化)。 我尝试遵循ggplot2图例进行组合geom_bar和geom_point的绘图,并在我的geom_point中引入了一个形状(没有这样做,我无法获得图例的点)。

library(ggplot2)
library(dplyr)
library(ggthemes)
library(plotly)

set.seed(369)

obs <- 6

values1 <- c(round(100 + rnorm(obs) * 10, 2))
values2 <- c(round(100 + rnorm(obs) * 10, 2))

df <- data.frame(Year = rep(2014:2019, 2*2), 
                 value = c(rep(values1, 2), rep(values2, 2)),
                 Indicator = rep(c("Indicator1", "Indicator2"), each = obs * 2),
                 Type = rep(c("Bar", "Point"), each = obs))

p <- ggplot(df, aes(value))

bars <- df  %>%
  filter(Type == "Bar")

points <- df  %>%
  filter(Type == "Point")

pl <- p +
  geom_bar(data = bars,
           aes(fill = Indicator, group = Indicator, x = Year, y = value), stat = "identity", position = "dodge") +
  geom_point(data = points,  aes(x = Year, y = value, group = Indicator, fill = Indicator, shape = "Change"), position = position_dodge(width = 0.9)) +
  theme_tufte() 

p
ggplotly(pl, tooltip = c("value"))

ggplotly有我想要的输出,但是图例有一个奇怪的分组。 有没有办法修复下表中的图例?

在此处输入图片说明

可能有更好的方法,但这是怎么回事:

library(tidyverse)

obs <- 6
values1 <- c(round(100 + rnorm(obs) * 10, 2))
values2 <- c(round(100 + rnorm(obs) * 10, 2))

df <- data.frame(Year = rep(2014:2019, 2*2), 
             value = c(rep(values1, 2), rep(values2, 2)),
             Indicator = rep(c("Indicator1", "Indicator2"), each = obs * 2),
             Type = rep(c("Bar", "Point"), each = obs))

bars   <- df  %>% filter(Type == "Bar")
points <- df  %>% filter(Type == "Point") %>% mutate(Year = 
                  ifelse(Indicator == "Indicator1", Year - 0.25, Year + 0.25))

 p <- ggplot(bars, aes(fill = Indicator, group = Indicator, x = Year, y = value)) +
      geom_bar(stat = "identity", position = "dodge", width = 1) 
 p <- p + geom_point(data = points, mapping = aes(fill = Indicator, x = 
      Year, y = value), shape = 21) + labs(x = "value") + labs(y = "value")
 p

在此处输入图片说明

我不知道ggplotly() ,但是构建单独的geom_bar()geom_point()绘图,然后使用get_legend()删除每个图例,然后使用plot_grid和完整绘图重新构建它们似乎是一个不错的选择。

library(tidyverse)

obs <- 6
values1 <- c(round(100 + rnorm(obs) * 10, 2))
values2 <- c(round(100 + rnorm(obs) * 10, 2))

df <- data.frame(Year = rep(2014:2019, 2*2), 
             value = c(rep(values1, 2), rep(values2, 2)),
             Indicator = rep(c("Indicator1", "Indicator2"), each = obs * 2),
             Type = rep(c("Bar", "Point"), each = obs))

bars   <- df  %>% filter(Type == "Bar")
points <- df  %>% filter(Type == "Point") %>% mutate(Year = 
ifelse(Indicator == "Indicator1", Year - 0.25, Year + 0.25),
                                                 IndicatorChange = Indicator)

p1 <- ggplot(points, mapping = aes(fill = IndicatorChange, x = Year, y = value )) + labs(x = "value") + labs(y = "value") +
      geom_point(shape = 21)
p1_leg <- get_legend(p1)
p2 <- ggplot(bars, aes(fill = Indicator, group = Indicator, x = Year, y = value)) +
      geom_bar(stat = "identity", position = "dodge") 
p2_leg <- get_legend(p2)

p_leg <- plot_grid(p1_leg, p2_leg, ncol = 1, nrow = 5) #toggle nrow to get right spacing between legends

p3 <-ggplot(bars, aes(fill = Indicator, group = Indicator, x = Year, y = value)) + geom_bar(stat = "identity", position = "dodge", width = 1) 
p3 <- p3 + geom_point(data = points, mapping = aes(fill = Indicator, x = Year, y = value), shape = 21) +
 labs(x = "value") + labs(y = "value")
p3 <- p3  + theme(legend.position="none")
p3
p <- plot_grid(p3, p_leg, ncol =2, nrow =2) #more toggling possible

p

在此处输入图片说明

我不知道这是否是您想要的(尽管应该修改图例的字体大小):

library(ggplot2)
library(dplyr)
library(ggthemes)
library(plotly)

set.seed(369)

obs <- 6

values1 <- c(round(100 + rnorm(obs) * 10, 2))
values2 <- c(round(100 + rnorm(obs) * 10, 2))

df <- data.frame(Year = rep(2014:2019, 2*2), 
                 value = c(rep(values1, 2), rep(values2, 2)),
                 Indicator = rep(c("Indicator1", "Indicator2"), each = obs * 2),
                 Type = rep(c("Bar", "Point"), each = obs))

p <- ggplot(df, aes(value))

bars <- df  %>%
  filter(Type == "Bar")

points <- df  %>%
  filter(Type == "Point")

points$Type1=paste(points$Indicator,"change",sep=",")


pl <- p +
  geom_bar(data = bars,
           aes(fill = Indicator, group = Indicator, x = Year, y = value), stat = "identity", position = "dodge") +
  geom_point(data = points,  
             aes(x = Year, y = value, group = Indicator, fill = Indicator, shape = "Change"), 
             position = position_dodge(width = 0.9)) +
  theme_tufte()+
  theme(legend.position="bottom")

pl <- p +
  geom_bar(data = bars,
           aes(fill = Indicator, group = Indicator,x = Year, y = value), stat = "identity", position = "dodge") +
  geom_point(data = points,  
             aes(x = Year, y = value,shape = Type1), 
             position = position_dodge(width = 0.9)) +
  theme_tufte()+
  theme(legend.position="bottom",
        legend.title=element_blank())
p

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM