gpt4 book ai didi

r - 如何修改 R 中条形图的图例?

转载 作者:行者123 更新时间:2023-12-02 01:15:26 25 4
gpt4 key购买 nike

在此条形图中,图例遮挡了其中的一部分。如何减小图例的大小?

My code:

`work.gender.marriage.table = table(work$marriage,work$gender)`

enter image description here

`barplot(work.gender.marriage.table,main = "Gender & Marriage",
beside = TRUE,
legend = rownames(work.gender.marriage.table))`

我的数据:

`structure(list(marriage = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L),    class = "factor", .Label = c("D", "M", "NM", "W")), gender = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"), val = c(12L, 61L, 78L, 56L, 71L, 33L, 86L, 93L)), .Names = c("marriage", "gender", "val"), row.names = c(NA, -8L), class = "data.frame")`

这是使用代码后的图像:

 barplot(work.gender.marriage.table,main = "Gender & Marriage",
beside = TRUE)

legend("topright",
legend = rownames(work.gender.marriage.table),
ncol = 2,
cex = 0.5)

图片:enter image description here

最佳答案

试试这个:

barplot(work.gender.marriage.table,main = "Gender & Marriage",
beside = TRUE)

然后将您的图例添加为:

legend("topright", 
legend = rownames(work.gender.marriage.table),
ncol = 2,
cex = 0.5)

根据您的数据:

df = structure(list(marriage = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), class = "factor", .Label = c("D", "M", "NM", "W")), gender = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("F", "M"), class = "factor"), val = c(12L, 61L, 78L, 56L, 71L, 33L, 86L, 93L)), .Names = c("marriage", "gender", "val"), row.names = c(NA, -8L), class = "data.frame")
df


dfM <- df[which(df$gender=="M"),]
dfF <- df[which(df$gender=="F"),]
dfN <- cbind(dfM[,3], dfF[,3])
colnames(dfN) <- c("M", "F")
rownames(dfN) <- dfM$marriage
dfN

barplot(dfN, beside=T, legend.text = rownames(dfN),
args.legend = list(x = "topleft", bty="n", cex = 0.7, ncol = 2))

enter image description here

关于r - 如何修改 R 中条形图的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com