Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R GG按错误顺序绘制的由两个变量编码的点的绘图_R_Ggplot2 - Fatal编程技术网

R GG按错误顺序绘制的由两个变量编码的点的绘图

R GG按错误顺序绘制的由两个变量编码的点的绘图,r,ggplot2,R,Ggplot2,我用ggplot2(R v4.0)创建了一个geom_点图,其中点由两个阶乘变量分类——“处理”(点的颜色)和“时间”(点的形状)。图例中正确指定了这两个变量的顺序(按时间顺序),但绘图中“一年中的时间”的各个点的顺序与图例中的顺序不匹配 我已经进行了广泛的搜索,但无法找到一种方法来改变点的顺序(这是有效的)。如果有任何见解,我将不胜感激 这是数据集: structure(list(Treatment = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,

我用ggplot2(R v4.0)创建了一个geom_点图,其中点由两个阶乘变量分类——“处理”(点的颜色)和“时间”(点的形状)。图例中正确指定了这两个变量的顺序(按时间顺序),但绘图中“一年中的时间”的各个点的顺序与图例中的顺序不匹配

我已经进行了广泛的搜索,但无法找到一种方法来改变点的顺序(这是有效的)。如果有任何见解,我将不胜感激

这是数据集:

structure(list(Treatment = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("Dry", 
"Drawdown-natural_inundation", "Drawdown-ewater", "Inundated-ewater"
), class = "factor"), TimeofYear = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L
), .Label = c("Aug.Sept", "Oct.Nov", "Dec.Jan", "Feb.Mar", "Apr.May"
), class = "factor"), predicted = c(9.09349409934063, 8.61564383754041, 
9.5157202467489, 9.60065460966658, 8.74270365273261, 8.24454674073168, 
9.18107640961278, 9.26907768652704, 9.57720737061331, 9.12472382159237, 
9.97898781938199, 10.060011680863, 8.41417418009182, 7.89531575895031, 
8.86880059605401, 8.95986948187825, 9.9788387001064, 9.54541280423986, 
10.3650624061018, 10.4430912953067), LB = c(7.87932611334752, 
6.7702957861824, 8.31338663092318, 8.22576777777575, 7.6828626517689, 
6.38852536865163, 8.02971988411625, 7.71306551965346, 8.54052535220469, 
7.57737204666066, 8.9056928198425, 8.71768315774855, 7.1098374938352, 
5.80048753638683, 7.65014600073047, 7.59589801908951, 8.41885808462554, 
7.56496679159677, 8.79707150140929, 8.70245298328878), UB = c(10.1636356619571, 
10.130238518332, 10.5823185717291, 10.8019388229304, 9.68727803838947, 
9.75355553294865, 10.2033291949699, 10.5990670630617, 10.512146724043, 
10.445314859168, 10.9475582299138, 11.2432055215275, 9.54184809547412, 
9.54077387884648, 9.93914051579127, 10.142034093227, 11.3259556840214, 
11.1803885732443, 11.7252108878204, 11.9324609229559), comb = c("Dry Aug.Sept", 
"Drawdown-natural_inundation Aug.Sept", "Drawdown-ewater Aug.Sept", 
"Inundated-ewater Aug.Sept", "Dry Oct.Nov", "Drawdown-natural_inundation Oct.Nov", 
"Drawdown-ewater Oct.Nov", "Inundated-ewater Oct.Nov", "Dry Dec.Jan", 
"Drawdown-natural_inundation Dec.Jan", "Drawdown-ewater Dec.Jan", 
"Inundated-ewater Dec.Jan", "Dry Feb.Mar", "Drawdown-natural_inundation Feb.Mar", 
"Drawdown-ewater Feb.Mar", "Inundated-ewater Feb.Mar", "Dry Apr.May", 
"Drawdown-natural_inundation Apr.May", "Drawdown-ewater Apr.May", 
"Inundated-ewater Apr.May")), row.names = c(NA, 20L), class = "data.frame")
这是我用来绘图的代码(带错误条的点):


谢谢。

你们非常接近。只是一些不好的分组

库(ggplot2)
ggplot(治疗前木质素,aes(x=治疗,
y=预测值)+
geom_点(aes)(颜色=处理,
形状=每年的时间),
尺寸=5,
位置=位置(减淡(0.3))+
几何误差条(aes(ymin=LB,
ymax=UB,
颜色=处理,
组=每年的时间,
宽度=0.2),
位置=位置(减淡(0.3))+
比例-形状-手册(值=c(15,16,17,18,21),
name=“每年的时间”,
标签=c(“八月/九月”、“十月/十一月”、“十二月/一月”、“二月/三月”、“四月/五月”))+
比例x离散(标签=c(“干”=“干”,
“水位下降-自然淹没”=“水位下降-自然淹没”,
“水位下降水量”=“水位下降水量”,
“淹没的水”=“淹没的水”))+
主题(legend.position=“底部”,legend.box=“垂直”)+
主题(axis.text.x=element\u text(角度=90,vjust=0.4,hjust=1))+
辅助线(形状=辅助线\图例(顺序=0),颜色=假)+
ylab(“木质素状况评分”)+
ggtitle(“木质素状况-预测值”)+
xlab(“治疗”)

谢谢,非常感谢Chuck p。我真的很感谢你的帮助。不用担心,让我挠头了。这对我来说一点都不明显。
ggplot(TreatPredLignum, aes(x = Treatment, y = predicted)) +
  geom_point(aes(colour = Treatment, shape = TimeofYear, group = comb), 
             size = 5, position = position_dodge(0.3)) +      
  geom_errorbar(aes(ymin = LB, ymax = UB, colour = Treatment, group = comb, width = 0.2), 
                position = position_dodge(0.3)) + 
  scale_shape_manual(values = c(15, 16, 17, 18, 21), name = "Time of Year", 
                     labels = c("Aug/Sept", "Oct/Nov", "Dec/Jan", "Feb/Mar", "Apr/May")) + 
  scale_x_discrete(labels = c("Dry" = "Dry", "Drawdown-natural_inundation" = "Ddown natural", 
                              "Drawdown-ewater"= "Ddown eWater", "Inundated-ewater"= "Inund eWater")) +
  theme(legend.position = "bottom", 
        legend.box      = "vertical",
        axis.text.x     = element_text(angle = 90, vjust=0.4, hjust=1)) +
  guides(shape = guide_legend(order = 1), 
         color = FALSE) +
  ylab("Lignum Condition Score") +
  ggtitle("Lignum Condition - predicted values") +
  xlab("Treatment")