Reputation: 27594
I'm sorry to ask a question that has been asked before on SO, but I'm trying to plot some simple data in ggplot2 and am having trouble binning the data along the x-axis. My data consists of visual elements in old books (diagrams, engravings, etc.), and I can plot the frequency of each type of visual element in each year:
#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")
This yields:
To make the data more legible, I want to bin the values along the x-axis by decade, but can't quite seem to get the call right:
#this doesn't
ggplot(data=df, aes(x=V1, y=V3)) +
geom_bar(aes(fill=V2),binwidth=10,stat="bin")
Running the latter code, I get:
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
Does anyone know how I can bin by decade along the x-axis? I would be grateful for any advice others can offer.
Upvotes: 0
Views: 2988
Reputation: 9123
In your situation, I find it easier to do some data manipulation before calling ggplot()
. I personally prefer these packages: dplyr
for data management and scales
for working with graphics, but you could do this using base
functions as well.
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()
Upvotes: 2