我有一个df
,我试图用geom_histogram
函数绘图:
structure(list(dFormat = c("11M 50S", "13M 30S", "14M 20S", "14M 40S",
"1M 10S", "1M 20S", "1M 40S", "1M 50S", "2M 10S", "2M 20S", "2M 30S",
"2M 40S", "2M 50S", "3M 0S", "3M 10S", "3M 20S", "3M 30S", "3M 40S",
"3M 50S", "4M 0S", "4M 10S", "4M 20S", "4M 30S", "4M 40S", "4M 50S",
"5M 0S", "5M 10S", "5M 20S", "5M 30S", "5M 40S", "5M 50S", "6M 0S",
"6M 10S", "6M 20S", "6M 30S", "6M 40S", "6M 50S", "7M 0S", "7M 10S",
"7M 20S", "7M 30S", "7M 40S", "7M 50S", "8M 0S", "8M 10S", "8M 20S",
"8M 30S", "8M 40S", "8M 50S", "9M 0S", "9M 10S", "9M 20S", "9M 30S"
), freq = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 6L, 7L, 10L, 20L, 22L, 47L, 39L, 82L, 66L, 121L, 107L, 162L,
145L, 208L, 162L, 240L, 162L, 228L, 200L, 197L, 159L, 185L, 126L,
157L, 113L, 123L, 73L, 61L, 49L, 33L, 23L, 18L, 11L, 16L, 5L,
6L, 4L, 2L, 2L)), .Names = c("dFormat", "freq"), row.names = c(NA,
-53L), class = "data.frame")
我正在尝试使用以下代码制作一个简单的直方图,但是我收到一个错误:
ggplot(df, aes(x = df$dFormat, y = df$freq)) +
geom_histogram(stat = "identity",binwidth = 50)
Warning: Ignoring unknown parameters: binwidth, bins, pad
查看我看到的文档:
Number of bins. Overridden by binwidth. Defaults to 30
使用binwidth
并不能解决问题。
将geom_histogram
更改为geom_bar
也无济于事。
In addition: Warning message:
geom_bar()no longer has a
{binwidth {1}} geom_histogram()parameter. Please use
我在这里看到了一个类似的问题,但没有用
也许@hadley会知道
答案 0 :(得分:0)
我认为Richard Telford的答案已经完成。 以下是我所相信的,您正在寻找:
df <- structure(list(dFormat = c("11M 50S", "13M 30S", "14M 20S", "14M 40S",
"1M 10S", "1M 20S", "1M 40S", "1M 50S", "2M 10S", "2M 20S", "2M 30S",
"2M 40S", "2M 50S", "3M 0S", "3M 10S", "3M 20S", "3M 30S", "3M 40S",
"3M 50S", "4M 0S", "4M 10S", "4M 20S", "4M 30S", "4M 40S", "4M 50S",
"5M 0S", "5M 10S", "5M 20S", "5M 30S", "5M 40S", "5M 50S", "6M 0S",
"6M 10S", "6M 20S", "6M 30S", "6M 40S", "6M 50S", "7M 0S", "7M 10S",
"7M 20S", "7M 30S", "7M 40S", "7M 50S", "8M 0S", "8M 10S", "8M 20S",
"8M 30S", "8M 40S", "8M 50S", "9M 0S", "9M 10S", "9M 20S", "9M 30S"
), freq = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 6L, 7L, 10L, 20L, 22L, 47L, 39L, 82L, 66L, 121L, 107L, 162L,
145L, 208L, 162L, 240L, 162L, 228L, 200L, 197L, 159L, 185L, 126L,
157L, 113L, 123L, 73L, 61L, 49L, 33L, 23L, 18L, 11L, 16L, 5L,
6L, 4L, 2L, 2L)), .Names = c("dFormat", "freq"), row.names = c(NA,
-53L), class = "data.frame")
ggplot(df, aes(x=dFormat,y=freq))+geom_bar(stat='identity')
答案 1 :(得分:0)
bins 和 binwidth 使用连续的 x 轴来制作频率图。您真正想要的是与不连续的变量 dFormat
的值成比例的条形图!您还遇到的问题是重叠的轴标签。使用 barplot()
绘制然后对轴标签进行排序:
attach(df)
barplot(freq,names.arg=dFormat,
cex.names=0.6,las=2, # order Axis labels-
col = "steelblue")
另一种可能性是将 df 转换为矩阵并将其传递给 barplot 函数。这将产生与上面相同的输出:
m <- t( matrix(df[,2],dimnames=df[1]) )
barplot(m,
cex.names=0.6,las=2,
col = "steelblue")
注意:虽然您无法更改每个条形中观察值的计数,但您可以通过 scale
更改条形的大小:
barplot(m,
cex.names=0.6,las=2,
col = 'steelblue',
space=5 #space between bars)