我对R很新,我似乎无法弄清楚如何获得我的多情节人物的传奇和标题,以使他们成为我想去的地方。我将不胜感激任何反馈。我对网络进行了研究,似乎找不到有用的东西。
附上我的代码:
pacman::p_load("tidyverse", "Cairo")
parameter_results <- readRDS("param_results_2014.RDS")
pie_chart <- function(parameter, title = parameter) {
parameter_df <- parameter_results %>%
select(results = parameter) %>% #keep only column for the parameter you want to plot
filter(results != "Not Applicable") %>%
count(results) %>% #
mutate(prop = prop.table(n), perc = paste0(round(prop * 100),"%"))
color_code <- c("Attaining" = "#99FF99", "Insufficient Information" = "#FFFF99", "Non Attaining" = "#FF9999")
parameter_df <- parameter_results %>%
select(results = parameter) %>% #keep only column for the parameter you want to plot
filter(results != "Not Applicable") %>%
count(results) %>% #
mutate(prop = prop.table(n), perc = paste0(round(prop * 100),"%"))
values <- vector(mode = "numeric", length = nrow(parameter_df))
labs <- vector(mode = "character", length = nrow(parameter_df))
colors <- vector(mode = "character", length = nrow(parameter_df))
for (i in seq_along(1:nrow(parameter_df))) {
values[[i]] <- parameter_df$prop[[i]] * 100
labs[[i]] <- parameter_df$perc[i]
colors[[i]] <- color_code[[parameter_df$results[[i]]]]
}
pie(x = values, labels = labs, col = colors, main = title,font=2,font.main=2)
mtext("Figure 2.10:Assessment Results for Key Parameters Associated with Water Supply Use,\nPercent(%) of 826 AUs", side = 3, line = -4, outer = TRUE,font=2)
}
CairoPDF(file = "multiple_pie_plot_example", width = 11.5, height = 11) # <--- you probably will need to change the height and width.
m <- matrix(c(1,2,3,
4,5,6,
7,7,7), nrow = 3, ncol = 3, byrow = TRUE)
layout(mat = m, heights = c(0.4, 0.4, 0.1))
par(mar = c(1,1,1,1))
pie_chart("Arsenic-HH", "Arsenic,Human Health")
pie_chart("Total Dissolved Solids","TDS")
pie_chart("Lead-HH - DWS", "Lead,Human Health")
pie_chart("Mercury-HH", "Mercury,Human Health")
pie_chart("DO Trout", " Nitrate")
plot.new()
legend(x = "bottom",inset = 0,legend = c("Attaining","Insufficient Information","Non Attaining"), fill = c("#99FF99", "#FFFF99", "#FF9999"),border="#000000")
plot.new()
dev.off()
这是我的情节:
这就是我想要的:
答案 0 :(得分:0)
我实际上无法重现您的数据,因为我无法访问RDS文件,但我会仔细查看par(?par
)。要获得外部的“图2.10”信息,您应该设置“oma”,即:
par(oma = c(bottom, left, top, right))
可以让你在所有内部情节之外添加边框。你需要找出所有4个点的适当数字。在par帮助中有一张图片说明了它正在做什么(默认值为0)。
如果您在顶部有空格,则可以使用title
放置一些内容 - 选择outer = TRUE
以获取该区域(而不是mtext)。
title("Figure 2.10:Assessment Results for Key Parameters Associated with Water Supply Use,\nPercent(%) of 826 AUs", side = 3, line = -4, outer = TRUE,font=2, outer = TRUE)
要修复图例,请将以下内容添加到图例中:
legend( ## previous stuff
x = "bottom",inset = 0,legend = c("Attaining","Insufficient Information","Non Attaining"), fill = c("#99FF99", "#FFFF99", "#FF9999"),border="#000000",
## new stuff:
horiz = TRUE, # switch to column format instead of rows
cex = 1.1, # scaling factor for text (it might make things bigger, I'd play with it)
bty = "n" # removes the box around the legend.
)
如果图例太小,您可能需要在那里玩一些东西。 (当我的数字和图例太小时,我通常只会将pdf缩小。我不知道它是否被视为黑客攻击,但它通常有效!)
移动单个标题:
边距空间以行数衡量。如果我没记错的话,标题会放在远离情节的第3行。你可以“修复”这两种方式(我会尝试两种方式)。首先,您可以手动调整绘图的限制,以确保圆圈上方和下方没有太多额外空间。然后,您可以更改文本所在的行。这是通过使用您的个人标题调用title()来完成的。在这里,您必须明确告诉它不要将标题放在外边距。对于每个情节,你会打电话给
title(your_individual_title, line = 1, outer = FALSE)
你可以玩这条线,直到你有你想要的间距。
要更改图例位置:
您的图例位于同一位置,因为您正在调用plot.new(),这会创建一个空白图。然后,您将图例添加到空白图中。要获取所需的图例,请在底部外边距中创建空间,然后使用选项xpd = NA调用图例。这可以控制图例放置位置的限制。尝试使插入为负(你需要使用它才能正确)将图例放在你想要的位置。
最终代码看起来应该是这样的(实际数字可能会有所不同):
pie_chart <- function(parameter, title = parameter) {
parameter_df <- parameter_results %>%
select(results = parameter) %>% #keep only column for the parameter you want to plot
filter(results != "Not Applicable") %>%
count(results) %>% #
mutate(prop = prop.table(n), perc = paste0(round(prop * 100),"%"))
color_code <- c("Attaining" = "#99FF99", "Insufficient Information" = "#FFFF99", "Non Attaining" = "#FF9999")
parameter_df <- parameter_results %>%
select(results = parameter) %>% #keep only column for the parameter you want to plot
filter(results != "Not Applicable") %>%
count(results) %>% #
mutate(prop = prop.table(n), perc = paste0(round(prop * 100),"%"))
values <- vector(mode = "numeric", length = nrow(parameter_df))
labs <- vector(mode = "character", length = nrow(parameter_df))
colors <- vector(mode = "character", length = nrow(parameter_df))
for (i in seq_along(1:nrow(parameter_df))) {
values[[i]] <- parameter_df$prop[[i]] * 100
labs[[i]] <- parameter_df$perc[i]
colors[[i]] <- color_code[[parameter_df$results[[i]]]]
}
pie(x = values, labels = labs, col = colors) # It doesn't look like this has an xlim or ylim option, so you're limited here.
title(main = title, font = 2, font.main = 2, outer = FALSE, line = 1) # move all the info relating to the titles to this command.
} CairoPDF(file =“multiple_pie_plot_example”,width = 11.5,height = 11)#&lt; ---您可能需要更改高度和宽度。
m <- matrix(c(1,2,3,
4,5,6,
7,7,7), nrow = 3, ncol = 3, byrow = TRUE)
layout(mat = m, heights = c(0.4, 0.4, 0.1))
par(mar = c(1,1,1,1), oma = c(4, 0, 4, 0)) # play with oma numbers
pie_chart("Arsenic-HH", "Arsenic,Human Health")
pie_chart("Total Dissolved Solids","TDS")
pie_chart("Lead-HH - DWS", "Lead,Human Health")
pie_chart("Mercury-HH", "Mercury,Human Health")
pie_chart("DO Trout", " Nitrate")
plot.new()
legend(x = "bottom",inset = 0,legend = c("Attaining","Insufficient Information","Non Attaining"), fill = c("#99FF99", "#FFFF99", "#FF9999"),border="#000000")
dev.off()
查看这些功能的帮助文件(标题,图例,标准杆和饼图)。您可能会在那里找到更多选项以获得您想要的内容。