Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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 优势比图上y轴标签的更改顺序_R_Ggplot2 - Fatal编程技术网

R 优势比图上y轴标签的更改顺序

R 优势比图上y轴标签的更改顺序,r,ggplot2,R,Ggplot2,我想绘制以下优势比的可视化图 这是我正在使用的代码 boxLabels = c("Past encounter", "Mutualism WVO:\nNon-hunting wildlife experiences", "Domination WVO:\nEthics of hunting", "Domination WVO:\nWildlife management", "Age", &quo

我想绘制以下优势比的可视化图

这是我正在使用的代码

boxLabels = c("Past encounter", "Mutualism WVO:\nNon-hunting wildlife experiences", "Domination WVO:\nEthics of hunting", "Domination WVO:\nWildlife management", "Age", "Education", "Negative attitudes toward box turtles", "Gender")

df <- data.frame(yAxis = length(boxLabels):1,
  boxOdds = 
   c(2.23189,1.315737,1.22866,.8197413,.9802449,.9786673,.6559005,.5929812),
  boxCILow = 
   c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142),
  boxCIHigh = 
  c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584)
)


(p <- ggplot(df, aes(x = boxOdds, y = boxLabels)) +
  geom_vline(aes(xintercept = 1), size = .25, linetype = 'dashed') +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = 
      .2, color = 'gray50') +
  geom_point(size = 3.5, color = 'orange') +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_x_continuous(breaks = seq(0,7,1) ) +
  coord_trans(x = 'log10') +
  ylab('') +
  xlab('Odds ratio (log scale)') +
  annotate(geom = 'text', y =1.1, x = 3.5, label ='Model p < 0.001\nPseudo 
R^2 = 0.10', size = 3.5, hjust = 0) + ggtitle('Intention to remove box 
turtles from the road')
)
boxLabels=c(“过去的遭遇”,“互惠主义”WVO:\n非狩猎野生动物体验”,“支配WVO:\n狩猎技巧”,“支配WVO:\n野生动物管理”,“年龄”,“教育”,“对箱龟的负面态度”,“性别”)

df要避免y标签的自动字母顺序,请按所需顺序转换为
factor()
。然后使用
scale\u y\u discrete(limits=rev)
获得正确方向的订单。完整示例:

boxLabels = c("Past encounter", "Mutualism WVO:\nNon-hunting wildlife experiences", "Domination WVO:\nEthics of hunting", "Domination WVO:\nWildlife management", "Age", "Education", "Negative attitudes toward box turtles", "Gender")

#Add a "ylabel" to dataframe
df <- data.frame(yAxis = length(boxLabels):1,
                 ylabel = boxLabels,
                 boxOdds = 
                   c(2.23189,1.315737,1.22866,.8197413,.9802449,.9786673,.6559005,.5929812),
                 boxCILow = 
                   c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142),
                 boxCIHigh = 
                   c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584)
)
#Convert the 'ylabel' into a factor with levels set by oder of boxlabels
df$ylabel<-factor(df$ylabel, levels = boxLabels)

#"boxlabel" is now the y. Intially give you the reverse order
(p <- ggplot(df, aes(x = boxOdds, y = ylabel)) +
    geom_vline(aes(xintercept = 1), size = .25, linetype = 'dashed') +
    geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = 
                     .2, color = 'gray50') +
    geom_point(size = 3.5, color = 'orange') +
    theme_bw() +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(breaks = seq(0,7,1) ) +
    coord_trans(x = 'log10') +
    ylab('') +
    xlab('Odds ratio (log scale)') +
    annotate(geom = 'text', y =1.1, x = 3.5, label ='Model p < 0.001\nPseudo 
R^2 = 0.10', size = 3.5, hjust = 0) + ggtitle('Intention to remove box 
turtles from the road')+
    #highlight one point as blue, as requested
    geom_point(data=df, 
               aes(x=boxOdds[1],y=ylabel[1]), 
               color='blue',
               size=3.5)
)
#Get correct order with scale_y_discrete()
p+scale_y_discrete(limits=rev)
boxLabels=c(“过去的遭遇”,“互惠主义”WVO:\n非狩猎野生动物体验”,“支配WVO:\n狩猎技巧”,“支配WVO:\n野生动物管理”,“年龄”,“教育”,“对箱龟的负面态度”,“性别”)
#将“ylabel”添加到数据帧

df x数据是否与y标签正确匹配?还是只希望标签移动?要按降序排列吗?要高亮显示,只需在绘图末尾添加另一个几何点(),并使用要高亮显示的点的索引。如果要突出显示第一个点,则:
+geom_点(data=df,aes(x=box赔率[1],y=boxLabels[1]),color='blue',size=3)
@SEAnalyst x数据与y标签匹配-我希望以特定顺序排列,因此它的顺序列在第一行的框标签中