如何使用 ggplot 可视化分组数据

how to use ggplot to visualise grouped data

我有以下代码生成以下结果:

years <- seq(1930, 2020, by = 10)
length(years)
years
labels <- paste(1 + years[-length(years)], years[-1], sep = "-")
length(labels)
labels
SP500 %>% mutate(decade = cut(SP500$Year,seq(1930,2020,by=10), labels = labels)) %>% 
  group_by(decade) %>% summarise(return = mean(`Annual\n% Change`))
# A tibble: 10 × 2
   decade    return
   <fct>      <dbl>
 1 1931-1940  0.014
 2 1941-1950  0.077
 3 1951-1960  0.124
 4 1961-1970  0.056
 5 1971-1980  0.058
 6 1981-1990  0.098
 7 1991-2000  0.157
 8 2001-2010  0.018
 9 2011-2020  0.121
10 NA         0.04 

我的问题是如何将此结果放入条形图或折线图中? 在过去的几个小时里,我一直在尝试这样做,但我一直收到错误,虽然我觉得答案很简单,但我似乎看不到它

years <- seq(1930, 2020, by = 10)
length(years)
labels <- paste(1 + years[-length(years)], years[-1], sep = "-")
length(labels)
SP500 %>% mutate(decade = cut(SP500$Year,seq(1930,2020,by=10), labels = labels)) %>% 
  group_by(decade) %>% summarise(return = mean(`Annual\n% Change`)) %>%
  ggplot(SP500, aes(x = decade, y = return)) +
  geom_col()

ggplot() 中的错误: !应使用 aes()aes_().

创建映射
SP500 %>% ggplot(
  ss <- SP500 %>% mutate(decade = cut(SP500$Year,seq(1930,2020,by=10))) %>% 
  group_by(decade) %>% summarise(return = mean(`Annual\n% Change`)), aes_(x=ss[,1], y= ss[,2]))  + geom_line()

非常感谢

您将数据操作的结果通过管道传递给 ggplot,但也将数据框的名称作为第一个参数传递给 ggplot。

记住这样做

data_frame %>% ggplot(aes(x, y))

和做的一样

ggplot(data = data_frame, mapping = aes(x, y))

但是在做

data_frame %>% ggplot(data_frame, aes(x, y))

和做的一样

ggplot(data = data_frame, mapping = data_frame, aes(x, y))

当然,您会收到错误消息,因为您无法将数据框传递给 mapping 参数。

所以你可以做到

SP500 %>% 
  mutate(decade = cut(SP500$Year,seq(1930,2020,by=10), labels = labels)) %>% 
  group_by(decade) %>% 
  summarise(return = mean(`Annual\n% Change`)) %>%
  ggplot(aes(x = decade, y = return)) +
  geom_col()

SP500 <- SP500 %>% 
  mutate(decade = cut(SP500$Year,seq(1930,2020,by=10), labels = labels)) %>% 
  group_by(decade) %>% 
  summarise(return = mean(`Annual\n% Change`))

ggplot(SP500, aes(x = decade, y = return)) +
  geom_col()

两者都导致:

上图是使用以下代码制作的,其中包含从您的问题中获取的数据。如果您将此代码块复制并粘贴到您的 R 控制台中,它会产生相同的图:

SP500 <- structure(list(decade = structure(1:9, .Label = c("1931-1940", 
"1941-1950", "1951-1960", "1961-1970", "1971-1980", "1981-1990", 
"1991-2000", "2001-2010", "2011-2020"), class = "factor"), return = c(0.014, 
0.077, 0.124, 0.056, 0.058, 0.098, 0.157, 0.018, 0.121)), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))

library(ggplot2)
ggplot(SP500, aes(decade, return)) + geom_col()