0

我很抱歉问一个之前在 SO 上问过的问题,但我试图 在 ggplot2中绘制一些简单的数据,并且无法沿 x 轴对数据进行分箱。我的数据由旧书中的视觉元素(图表、版画等)组成,我可以绘制每年每种视觉元素的频率:

#this works
df <- read.table("cleaned_estc_visuals.txt",
                 header = F,
                 sep = "\t")

ggplot(data=df, aes(x=V1, y=V3)) + 
  geom_bar(aes(fill=V2),stat="identity") +
  labs(title = "Visuals in Early Modern Books",fill="") +
  xlab("Year") + 
  ylab("Titles") 

这产生: 在此处输入图像描述

为了使数据更清晰,我想将 x 轴上的值按十年排列,但似乎无法正确调用:

#this doesn't
ggplot(data=df, aes(x=V1, y=V3)) + 
  geom_bar(aes(fill=V2),binwidth=10,stat="bin")

运行后面的代码,我得到:

Mapping a variable to y and also using stat="bin".
  With stat="bin", it will attempt to set the y value to the count of cases in each group.
  This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
  If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
  If you want y to represent values in the data, use stat="identity".
  See ?geom_bar for examples. (Deprecated; last used in version 0.9.2)
Error in pmin(y, 0) : object 'y' not found

有谁知道我如何沿 x 轴按十年分档?对于其他人可以提供的任何建议,我将不胜感激。

4

1 回答 1

2

在您的情况下,我发现在调用ggplot(). 我个人更喜欢这些包:dplyr用于数据管理和scales处理图形,但您也可以使用base函数来实现。

library(dplyr)
library(scales)

df2 <- df %>%
  mutate(decade = floor(V1 / 10) * 10) %>% 
  group_by(decade, V2) %>%
  summarise(V3 = sum(V3)) %>%
  filter(decade != 1800)


ggplot(df2, aes(x = decade, y = V3)) +
  geom_bar(aes(fill = V2), stat = "identity") +
  labs(x = "Decade", y = "Titles", title = "Visuals in Early Modern Books") +
  scale_x_continuous(breaks = pretty_breaks(20)) # using scales::pretty_breaks()
于 2014-11-07T16:45:19.377 回答