You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言分组柱状图(grouped bar chart)中移除Y轴

移除R语言分组柱状图Y轴的方法

问题(翻译)

我希望从使用R语言绘制的分组柱状图中移除Y轴。请查看下方可复现代码,是否存在实现该需求的方法?

解决方案

在基础绘图的barplot()函数里添加yaxt="n"参数就能直接移除Y轴,这个参数的作用是禁止绘制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,避免变量名冲突
colors <- c("dodgerblue3","red3")
barplot(as.matrix(data), 
    main="",
    beside = T, ylab = "", ylim = range(0,65),  col=colors, border=F,
    yaxt="n"  # 新增参数:隐藏Y轴
)

legend(5,60,  legend=c("Fox News", "CNN"), 
   fill=colors, horiz = T, 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%")

额外提个小建议:尽量别用attach()函数,后续加载其他数据时很容易出现变量名冲突,直接用data$列名或者with()包裹代码块会更稳妥。

内容的提问来源于stack exchange,提问作者Data and AI Nerd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:36