条形图 - R 中的直方图
Bar Chart - Histogram in R
我想绘制我的数据集的直方图,其中包含两列,一列带有名称,另一列带有如下所示的值:
City Values
Graz 2799
Innsbruck 2802
Klagenfurt 2591
Linz 3016
Salzburg 2716
Wien 2252
我想创建一个条形直方图,显示水平轴上具有 Names 变量的每个名称的值。
我试过以下代码:
ggplot(data) +
geom_bar(aes(x=City))
但我得到的不是我想要的,看起来像这样:
你知道我该如何解决吗?或者您对任何其他情节有什么建议可以向我展示与我想要的结果相似的结果吗?
这是带有您的值的条形图。
ggplot(data, aes(x=City, y=Values)) +
geom_bar(stat = "identity")
来自文档:
By default, geom_bar uses stat="bin" . This makes the height of each
bar equal to the number of cases in each group, and it is incompatible
with mapping values to the y aesthetic. If you want the heights of the
bars to represent values in the data, use stat="identity" and map a
value to the y aesthetic.
在base R
中,我们可以使用barplot
barplot(Values ~ City, df1)
-输出
数据
df1 <- structure(list(City = c("Graz", "Innsbruck", "Klagenfurt", "Linz",
"Salzburg", "Wien"), Values = c(2799L, 2802L, 2591L, 3016L, 2716L,
2252L)), class = "data.frame", row.names = c(NA, -6L))
我想绘制我的数据集的直方图,其中包含两列,一列带有名称,另一列带有如下所示的值:
City Values
Graz 2799
Innsbruck 2802
Klagenfurt 2591
Linz 3016
Salzburg 2716
Wien 2252
我想创建一个条形直方图,显示水平轴上具有 Names 变量的每个名称的值。 我试过以下代码:
ggplot(data) +
geom_bar(aes(x=City))
但我得到的不是我想要的,看起来像这样:
你知道我该如何解决吗?或者您对任何其他情节有什么建议可以向我展示与我想要的结果相似的结果吗?
这是带有您的值的条形图。
ggplot(data, aes(x=City, y=Values)) +
geom_bar(stat = "identity")
来自文档:
By default, geom_bar uses stat="bin" . This makes the height of each bar equal to the number of cases in each group, and it is incompatible with mapping values to the y aesthetic. If you want the heights of the bars to represent values in the data, use stat="identity" and map a value to the y aesthetic.
在base R
中,我们可以使用barplot
barplot(Values ~ City, df1)
-输出
数据
df1 <- structure(list(City = c("Graz", "Innsbruck", "Klagenfurt", "Linz",
"Salzburg", "Wien"), Values = c(2799L, 2802L, 2591L, 3016L, 2716L,
2252L)), class = "data.frame", row.names = c(NA, -6L))