在 R 中绘制数据框的历史图时出错

问题描述 投票:0回答:2

我有一个看起来像这样的数据:

cis_distance_freq4
# A tibble: 6 × 2
  distance  freq
     <dbl> <dbl>
1        0    NA
2     1000  4380
3    10000  4381
4    40000  4535
5    80000  4536
6  1000000  4558

hist(cis_distance$freq)

我想以 x 轴为距离,y 轴为频率的方式绘制。 有谁知道如何绘制这个。

r dataframe plot histogram visualization
2个回答
2
投票

您可以使用

~
中的公式
barplot
符号在基数 R 中执行此操作:

barplot(freq ~ distance, data = cis_distance_freq4, 
        ylim = c(0, 5000),
        xlab = "Distance", ylab = "Frequency")

或者流行的

ggplot2

ggplot(cis_distance_freq4[-1,], aes(x = as.factor(distance), y = freq)) +
  geom_bar(stat = "identity") + 
  ylim(c(0,5000)) +
  ylab("Distance") + xlab("Frequency")


1
投票

这是

ggplot()
解决方案,使用
geom_bar()
stat="identity"

library(ggplot2)
cis_distance_freq4 <- tibble::tribble(
~distance,  ~freq,
      0,    NA,
   1000,  4380,
  10000,  4381,
  40000,  4535,
  80000,  4536,
1000000,  4558)


ggplot(cis_distance_freq4, aes(x=as.factor(distance), y=freq)) + 
  geom_bar(stat="identity", width=.99)
#> Warning: Removed 1 rows containing missing values (`position_stack()`).

创建于 2023-03-28 与 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.