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 軸に沿って値を 10 年ごとにビンに入れたいのですが、呼び出しが正しく行われていないようです。

#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軸に沿って10年ごとにビン化する方法を知っている人はいますか? 他の人が提供できるアドバイスに感謝します。

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 に答える