从分组柱状图中删除Y轴。

huangapple go评论92阅读模式
英文:

How to remove y-axis from grouped bar chart

问题

我想从我的图表中移除y轴。请查看下面的可重现代码。有没有办法做到这一点?

data <- structure(list(
  A = c(53, 39),
  B = c(30, 36),
  C = c(12, 19),
  D = c(5, 6)),
  .Names = c("Poor", "Fair", "Good", "Excellent"),
  class = "data.frame",
  row.names = c(NA, -2L))

attach(data)
print(data)
colors <- c("dodgerblue3", "red3")
barplot(as.matrix(data),
  main = "",
  beside = TRUE, ylab = "", ylim = range(0, 65), col = colors, border = FALSE)

legend(5, 60, legend = c("Fox News", "CNN"),
  fill = colors, horiz = TRUE, bty = 'n')
text(1.5, 55, "53%")
text(2.5, 41, "39%")
text(4.5, 32, "30%")
text(5.5, 38, "36%")
text(7.5, 14, "12%")
text(8.5, 21, "19%")
text(10.5, 7, "5%")
text(11.5, 8, "6%")

这是你提供的代码的翻译部分。

英文:

I would like to remove the y-axis from my graph. Please see reproducible code below. Is there a way to do this?

data &lt;- structure(list(
A= c(53, 39),
B = c(30, 36), 
C = c(12, 19), 
D = c(5, 6)), 
.Names = c(&quot;Poor&quot;, &quot;Fair&quot;, &quot;Good&quot;, &quot;Excellent&quot;), 
class = &quot;data.frame&quot;, 
row.names = c(NA, -2L))

attach(data)
print(data)
colors &lt;- c(&quot;dodgerblue3&quot;,&quot;red3&quot;)
barplot(as.matrix(data), 
    main=&quot;&quot;,
    beside = T, ylab = &quot;&quot;, ylim = range(0,65),  col=colors, border=F)

legend(5,60,  legend=c(&quot;Fox News&quot;, &quot;CNN&quot;), 
   fill=colors, horiz = T, bty=&#39;n&#39;)
text(1.5, 55, &quot;53%&quot;)
text(2.5, 41, &quot;39%&quot;)
text(4.5, 32, &quot;30%&quot;)
text(5.5, 38, &quot;36%&quot;)
text(7.5, 14, &quot;12%&quot;)
text(8.5, 21, &quot;19%&quot;)
text(10.5, 7, &quot;5%&quot;)
text(11.5, 8, &quot;6%&quot;)`

答案1

得分: 1

?par中,您可以找到所有的设置,这将指向yaxt=&quot;n&quot;

您还可以在绘图时保存柱状图的位置,并使用它来自动生成您的百分比标签。

colors &lt;- c(&quot;dodgerblue3&quot;,&quot;red3&quot;)
bp &lt;- barplot(as.matrix(data),  main=&quot;&quot;, beside = T, ylab = &quot;&quot;,
                  ylim = range(0,65), col=colors, border=FALSE, yaxt=&quot;n&quot;)
text(x=bp, y=unlist(data) + 2, labels=unlist(data))
legend(5,60,  legend=c(&quot;Fox News&quot;, &quot;CNN&quot;), fill=colors, horiz = TRUE, bty=&#39;n&#39;, border=0)
英文:

From ?par, you can find all the settings, which would point you to yaxt=&quot;n&quot;:

You can also save off the barplot positions when plotting, and use that to generate your percentage labels automatically.

colors &lt;- c(&quot;dodgerblue3&quot;,&quot;red3&quot;)
bp &lt;- barplot(as.matrix(data),  main=&quot;&quot;, beside = T, ylab = &quot;&quot;,
              ylim = range(0,65), col=colors, border=FALSE, yaxt=&quot;n&quot;)
text(x=bp, y=unlist(data) + 2, labels=unlist(data))
legend(5,60,  legend=c(&quot;Fox News&quot;, &quot;CNN&quot;), fill=colors, horiz = TRUE, bty=&#39;n&#39;, border=0)

huangapple
  • 本文由 发表于 2023年5月17日 11:45:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定