【问题标题】:plottext on horizontal bar in RR中水平条上的绘图文本
【发布时间】:2018-05-19 08:05:53
【问题描述】:

我正在尝试绘制条形图并希望在水平线末端显示百分比.. 无法得到.. 是什么错误

y <- c('Others(27%)','Environment','Family Life & children','Combating terrorism','Pensions',
   'Europe', 'Housing', 'Education', 'Tax','Benefits','Immigration & Assylum','Health','Economy')
x <- c(27,10,12,15,18,20,20,21,21,25,42,46,49)

mylabels <- c('....27%','10%','12%','15%','18%','20%','20%','21%','21%','25%','42%','46%','49%')

par(mar=c(5,10,4,2)+0.1)
usr <- par( "usr" )
my_bar<-barplot(x,main="British Voters Election Priority (2015)", horiz=TRUE,las = 1,
        names.arg=y, xlim=c(0, 60), border = 'black',         
col=rev(topo.colors(14)),
        xlab="Percentage (%)")  
abline(v=x, col='grey', type="l", lty=2)
#text(myvect1, b[1,], mylabels, col="white", adj=1) 
text(my_bar, mylabels, cex=1) 

【问题讨论】:

    标签: r ggplot2 statistics


    【解决方案1】:

    如果您希望数字显示在条的末尾,只需从“mylabels”中提取数字字符以获得适当的 x 坐标并设置 pos = 4 以将它们显示在条的右侧。至于条形的颜色,只需选择一些适合您意愿的调色板或手动指定所有 13 种颜色。整个网络上有大量 R 的不同颜色模式的备忘单。

     my_bar<-barplot(x,main="British Voters Election Priority (2015)",      horiz=TRUE,las = 1,
                names.arg=y, xlim=c(0, 60), border = 'black',         
                col = palette(rainbow(13)),
                xlab="Percentage (%)")  
     abline(v=x, col='grey', type="l", lty=2)
     #text(myvect1, b[1,], mylabels, col="white", adj=1) 
     text(as.numeric(gsub("\\D", "", mylabels)), my_bar, mylabels, cex=1, pos = 4) 
    

    【讨论】:

    【解决方案2】:

    您没有为应该绘制 tekst 的位置指定 x 坐标。您的代码现在将 my_bar 的值用于 x 坐标和 y 坐标。如果我是正确的,你想要这样的东西。

    只需在最后使用这行代码即可:

    text(60, my_bar, mylabels, cex=1, pos = 2)  
    

    【讨论】:

    • 非常感谢...如何在外面的每个栏的末尾添加价值
    • 我如何区分前两个条的颜色.. 它们看起来几乎相似
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多