[英]Very simple - Histogram in R
I am struggling to understand how I can get the following data into a histogram: 我正在努力了解如何将以下数据转换为直方图:
NSP <- c(1380, 6003, 1827, 661, 331, 156, 97, 73, 58)
hist(NSP)
Each number should represent one bar in the exactly same order. 每个数字应以完全相同的顺序代表一个小节。 I tried to use ggplot but failed to get the frequencies on the Y axis.
我尝试使用ggplot,但未能获得Y轴上的频率。
Many thanks in advance! 提前谢谢了!
If you are trying to use ggplot2
, then you likely want geom_col
. 如果您尝试使用
ggplot2
,则可能需要geom_col
。 Note that you will need to specify x-positions for the bars. 请注意,您将需要指定条的x位置。 Here, I am just specifying them as
1:length(NSP)
using seq_along
: 在这里,我只是使用
seq_along
将它们指定为1:length(NSP)
:
ggplot(
mapping = aes(y = NSP
, x = seq_along(NSP))) +
geom_col() +
scale_x_continuous(breaks = seq_along(NSP))
If you have other labels for the breaks, you can specify them, eg: 如果您有其他休息时间标签,则可以指定它们,例如:
ggplot(
mapping = aes(y = NSP
, x = seq_along(NSP))) +
geom_col() +
scale_x_continuous(breaks = seq_along(NSP)
, labels = LETTERS[seq_along(NSP)])
Note, however, that ggplot
generally works much more smoothly when your data are in a data.frame. 但是请注意,当数据位于data.frame中时,
ggplot
通常会更加流畅。 This may be a more flexible approach for you, and allow more succinct naming of the column locations (eg, if they are not evenly spaced, you could have one column for the actual location, and one column for the label). 这对您来说可能是一种更灵活的方法,并且可以更简洁地命名列位置(例如,如果它们之间的间距不均匀,则实际位置可以为一列,标签可以为一列)。
NSP_df <- data.frame(Location = LETTERS[seq_along(NSP)]
, Count = NSP)
ggplot(
NSP_df
, aes(y = Count
, x = Location)) +
geom_col()
Maybe what you want is barplot(NSP)
? 也许您想要的是
barplot(NSP)
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.