英文:
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 <- 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 = T, ylab = "", ylim = range(0,65), col=colors, border=F)
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%")`
答案1
得分: 1
从?par
中,您可以找到所有的设置,这将指向yaxt="n"
:
您还可以在绘图时保存柱状图的位置,并使用它来自动生成您的百分比标签。
colors <- c("dodgerblue3","red3")
bp <- barplot(as.matrix(data), main="", beside = T, ylab = "",
ylim = range(0,65), col=colors, border=FALSE, yaxt="n")
text(x=bp, y=unlist(data) + 2, labels=unlist(data))
legend(5,60, legend=c("Fox News", "CNN"), fill=colors, horiz = TRUE, bty='n', border=0)
英文:
From ?par
, you can find all the settings, which would point you to yaxt="n"
:
You can also save off the barplot positions when plotting, and use that to generate your percentage labels automatically.
colors <- c("dodgerblue3","red3")
bp <- barplot(as.matrix(data), main="", beside = T, ylab = "",
ylim = range(0,65), col=colors, border=FALSE, yaxt="n")
text(x=bp, y=unlist(data) + 2, labels=unlist(data))
legend(5,60, legend=c("Fox News", "CNN"), fill=colors, horiz = TRUE, bty='n', border=0)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论