ggplotly中结合了geom_bar和geom_point图例

时间:2018-04-04 06:45:11

标签: r ggplot2 ggplotly

我正在尝试获得一个组合的条形图+点图表,其中包含两个条形图的不同指标)和点(指标的变化)。我尝试跟随ggplot2 legend for plot combining geom_bar and 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有我想要的输出,但是图例有一个奇怪的分组。有没有办法在下面的图表中修复图例?

enter image description here

3 个答案:

答案 0 :(得分:1)

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

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

enter image description here

答案 1 :(得分:1)

我不知道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

enter image description here

答案 2 :(得分:0)

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

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