1

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

在此处输入图像描述

4

3 回答 3

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

在此处输入图像描述

于 2018-04-05T03:59:07.003 回答
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

在此处输入图像描述

于 2018-04-04T07:38:59.673 回答
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
于 2018-04-04T14:34:34.937 回答