有谁知道如何创建散点图以在 PRISM 的图形板中R
创建像这样的图:
我尝试使用箱线图,但它们没有按照我想要的方式显示数据。graphpad 可以生成的这些列散点图对我来说更好地显示了数据。
任何建议,将不胜感激。
正如@smillig 提到的,您可以使用 ggplot2 来实现这一点。下面的代码很好地再现了您所追求的情节 - 警告它非常棘手。首先加载 ggplot2 包并生成一些数据:
library(ggplot2)
dd = data.frame(values=runif(21), type = c("Control", "Treated", "Treated + A"))
接下来更改默认主题:
theme_set(theme_bw())
现在我们建立情节。
构造一个基础对象 - 不绘制任何内容:
g = ggplot(dd, aes(type, values))
补充几点:根据类型调整默认抖动并更改字形:
g = g + geom_jitter(aes(pch=type), position=position_jitter(width=0.1))
添加“盒子”:计算盒子的结束位置。在这种情况下,我选择了平均值。如果您不想要该框,请省略此步骤。
g = g + stat_summary(fun.y = function(i) mean(i),
geom="bar", fill="white", colour="black")
添加一些误差线:计算上限/下限并调整条形宽度:
g = g + stat_summary(
fun.ymax=function(i) mean(i) + qt(0.975, length(i))*sd(i)/length(i),
fun.ymin=function(i) mean(i) - qt(0.975, length(i)) *sd(i)/length(i),
geom="errorbar", width=0.2)
显示绘图
g
stat_summary
即时计算所需的值。您还可以创建单独的数据框并使用geom_errorbar
and geom_bar
。如果您不介意使用该软件包,有一种简单的方法可以使用和ggplot2
制作类似的图形。使用示例数据:geom_boxplot
geom_jitter
mtcars
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot() + geom_jitter() + theme_bw()
生成以下图形:
可以在此处查看文档:http: //had.co.nz/ggplot2/geom_boxplot.html
我最近遇到了同样的问题,并找到了自己的解决方案,使用ggplot2
. 例如,我创建了数据集的一个子chickwts
集。
library(ggplot2)
library(dplyr)
data(chickwts)
Dataset <- chickwts %>%
filter(feed == "sunflower" | feed == "soybean")
由于geom_dotplot()
无法将点更改为符号,我使用geom_jitter()
如下:
Dataset %>%
ggplot(aes(feed, weight, fill = feed)) +
geom_jitter(aes(shape = feed, col = feed), size = 2.5, width = 0.1)+
stat_summary(fun = mean, geom = "crossbar", width = 0.7,
col = c("#9E0142","#3288BD")) +
scale_fill_manual(values = c("#9E0142","#3288BD")) +
scale_colour_manual(values = c("#9E0142","#3288BD")) +
theme_bw()
这是最后的情节:
有关更多详细信息,您可以查看此帖子:
http://withheadintheclouds1.blogspot.com/2021/04/building-dot-plot-in-r-similar-to-those.html?m=1