R ggplot2:对象';y';未找到stat=";“银行标识代码”;

R ggplot2:对象';y';未找到stat=";“银行标识代码”;,r,plot,ggplot2,geom-bar,R,Plot,Ggplot2,Geom Bar,很抱歉,我问了一个问题,但我正试图在ggplot2中绘制一些,在沿x轴组合数据时遇到了问题。我的数据包括旧书中的视觉元素(图表、雕刻等),我可以绘制每年每种视觉元素的频率: #this works df <- read.table("cleaned_estc_visuals.txt", header = F, sep = "\t") ggplot(data=df, aes(x=V1, y=V3)) + geom_b

很抱歉,我问了一个问题,但我正试图在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") 
运行后一个代码,我得到:

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轴按十年进行装箱吗?如果其他人能提供任何建议,我将不胜感激。

在您的情况下,我发现在调用
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()
库(dplyr)
图书馆(比例尺)
df2%
突变(十年=下限(V1/10)*10)%>%
按(十年,V2)分组%>%
总结(V3=总和(V3))%>%
过滤器(十进位!=1800)
ggplot(df2,aes(x=decade,y=V3))+
几何图形栏(aes(填充=V2),stat=“identity”)+
实验室(x=“十年”,y=“标题”,title=“早期现代书籍中的视觉效果”)+
缩放x_连续(打断=漂亮的打断(20))#使用缩放::漂亮的打断()

这看起来很棒,@DMC!编辑后的代码更清晰;谢谢你的改变。只是一个简单的问题:有没有一个简单的方法来删除最后的酒吧?我相信这是1800:1810的总和,因为在这个范围内只有一年(1800),这可能是欺骗。我会在你的代码第1行的df呼叫中呼叫哪个?最后一组将是1800-1809年(不是1800-1810年)的总和啊,谢谢你的澄清和筛选!这很有帮助!
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()