r - ggplot中带有误差线的点图改变了数据的呈现顺序
问题描述
哦,R的智者!
我在 ggplot2 中创建了一个带有误差线的点图。在大多数情况下,我让它按预期工作。然而,它似乎“随机”(据我所知)颠倒了它呈现我分析的两种材料的数据的顺序。
这是我的代码:
data<-data.frame("Mat_art"=c("Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners", "Apples", "Naners"), "CompIDinv"=c(17, 17, 16, 16, 15, 15, 14, 14, 13, 13, 12, 12, 11, 11, 10, 10), "Comp"=c("Cheese", "Cheese", "Mayo", "Mayo", "Ketchup", "Ketchup", "Glue", "Glue", "Tofu", "Tofu", "Ranch", "Ranch", "Marmite", "Marmite", "Butter", "Butter"), "Prom"=c(1.15968339356004, 1.09598175499146, 0.606871622602421, 0, 0.477223208284233, 0, 1.52931048485049, 0.499586619837113, 1.33614656021619, 1.95359709169813, 1.22093637869439, 2.34963432630937, 1.35262980291428, 1.69298650050846, 1.35086700266383, 1.24031665670217), "infCI95"=c(0.775987209086803, 0.727123902187271, 0.388630354128953, 0, 0.406186682979503, 0, 1.1431692694034, 0.318904748424858, 1.19987305571909, 1.03758502964521, 1.11361766267652, 2.09487026135963, 1.24530044274135, 0.543063809010205, 0.993468721657989, 0.695707000558221), "supCI95"=c(1.54337957803327, 1.46483960779564, 0.825112891075889, 0, 0.548259733588963, 0, 1.91545170029758, 0.680268491249369, 1.47242006471328, 2.86960915375105, 1.32825509471225, 2.60439839125911, 1.4599591630872, 2.84290919200671, 1.70826528366967, 1.78492631284613), "Color"=c("4", "3", "2", "2", "2", "2", "4", "2", "4", "4", "4", "4", "4", "4", "4", "4"), "Etiqueta"=c("Immobilization", "Conservation", "Degradation", "Degradation", "Degradation", "Degradation", "Immobilization", "Degradation", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization", "Immobilization"))
data$Comp<-as.factor(data$Comp)
data$Mat_art<-as.factor(data$Mat_art)
data$Etiqueta<-as.factor(data$Etiqueta)
ggplot(data = data, aes(x = reorder(Comp,CompIDinv), y = Prom, ymin = infCI95, ymax = supCI95, colour = reorder(Etiqueta, Color), shape=Mat_art)) +
scale_shape_manual(values=c(16, 2)) +
geom_point(position = position_dodge(width = 0.75), size = 2) +
geom_errorbar(position = position_dodge(width = 0.75), width = 0.5) +
scale_colour_manual(values = c("#0072B2", "#009E73", "#D55E00")) +
geom_hline(yintercept=1, linetype="dashed", color = "black", size=1) +
labs(x = "Dip", y = "Yummyness", colour = "Behavior", shape = "Material") +
coord_flip() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))
这是我从中得到的情节:
正如你所看到的,对于“Cheese”,Apples 先出现,但随后它首先切换到 Naners,直到“Glue”,然后再切换到最后。我的意图是让苹果永远排在第一位(对那些更喜欢 naners 的人没有冒犯的意思:D)。这种情况在完整数据集中发生了几次,其中这些是实际值。当然,这不是将你的纳米人浸入胶水中,别担心。
无论如何,如果您愿意帮助我解决这个问题,我将非常感激!
解决方案
我已经修改了您的代码,主要是先重新排序因子而不是 within ggplot
:
data$Comp<-as.factor(data$Comp)
data$Mat_art<-factor(data$Mat_art, levels = c("Naners", "Apples"))
data$Etiqueta<-factor(data$Etiqueta, levels = c("Degradation", "Conservation", "Immobilization"))
ggplot(data = data, aes(x = reorder(Comp,CompIDinv), y = Prom, ymin = infCI95, ymax = supCI95,
colour = Etiqueta, shape=Mat_art
)) +
scale_shape_manual(values=c(16, 2)) +
geom_point(position = position_dodge(width = 0.75), size = 2) +
geom_errorbar(position = position_dodge(width = 0.75), width = 0.5) +
scale_colour_manual(values = c("#0072B2", "#009E73", "#D55E00")) +
geom_hline(yintercept=1, linetype="dashed", color = "black", size=1) +
labs(x = "Dip", y = "Yummyness", colour = "Behavior", shape = "Material") +
coord_flip() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))
推荐阅读
- python - django电子商务网站中的“TypeError at / argument 1 must be str, not PosixPath”
- mysql - 在表中插入新数据而不删除现有数据
- javascript - 从网页中抓取 javascript 变量
- css - :first-child 嵌套元素
- c++ - 为什么 C++ 单元测试在程序中失败
- excel - VBA:无论索引排序如何,都匹配结果?
- reactjs - 如何在 React 中同时延迟加载多个组件
- cuda - 尝试使用 cuSPARSE 的 cusparseCsr2cscEx2() 函数执行矩阵转置时出现内部错误
- python - django 和 instapy 集成
- excel - 从 URL 中提取值并将其设置为变量