如何在 R 中绘制不是盒须图的图

How to Draw a Plot in R Which is Not a Box-and-Whiskers Plot

我有一个大型数据集,其中包含如下数据:

      Age          Ethnicity          Gender        Crude_Rate 
      20-24        Pacific Islander   Female        39.2   
      55-59        Caucasian          Male          100.3
      60-64        African            Female        3.2
      30-34        Asian              Male          2.8

我正在尝试在 R 上绘制三个图:

我正在绘制图,但它是一个盒须图,上面有点,这不是我想要的。我只想绘制点,即散点图。

我使用的代码是:

melanoma <-read.csv("Melanoma Age.csv", header=TRUE, na.strings = c("Not     Applicable"))
melanoma<-na.omit(melanoma)


plot(melanoma$Gender, melanoma$Crude_Rate,las=2, ylab="Crude Rate",    xlab="Gender", main="Crude Rates for Both Genders")
plot(melanoma$Age, melanoma$Crude_Rate,las=2, ylab="Crude Rate", main="Crude Rates for Different Age Groups")
plot(melanoma$Ethnicity, melanoma$Crude_Rate,las=2, ylab="Crude Rate", main="Crude Rates for Different Ethnicities")

我不明白我做错了什么,因为通常当我这样做时我会得到一个散点图,而不是上面有点的盒须图。

在此先感谢您的帮助

如果您的 x 值是一个因子变量,您可以生成这样的散点图:

#a reproducible example
set.seed(42)
x <- factor(sample(c("A", "B"), 20, TRUE))
y <- rnorm(20)

#use plot.default explicitly to avoid using plot.factor implicitly
plot.default(y ~ x, type = "p", 
             xlim = range(as.integer(unique(x))) + c(-0.4, 0.4), 
             xaxt = "n")
axis(1, at = seq_along(levels(x)), labels = levels(x))

我将尝试向您展示如何根据年龄绘制粗率。同样,您可以绘制其他图。为简单起见,我逐步展示了创建过程

input = NULL
input$Age = rep(c("20-24","55-59","60-64","30-34"),4)
input$Crude_Rate = runif(16)
input = as.data.frame(input)

library(dplyr)
Byage <- input %>% 
        group_by(Age) %>% 
        summarise(n = n(), Crude_Rate = mean(Crude_Rate))
plot1 <-ggplot(input, aes(Age, Crude_Rate))
plot1 <-plot1 + geom_jitter(width = 0.5)
plot1 <-plot1 + geom_point(data = Byage, colour = "red", size = 4)
plot1 <-plot1 + geom_text(aes(y = -0.1, label = paste0("n = ", n)), Byage, size = 3)

这将生成一个漂亮的散点图,其中年龄组为 x 轴,Crude_Rate 为 y 轴,底部为每个组中的样本数。

ScatterPlot of Age vs Crude_Rate with each age group mean Information

您还可以使用汽车包和函数散点图。这将为您提供图表上的额外内容(例如方框和胡须以及平滑器),但这些可以很容易地删除。