R 如何将单个值的表示方式从条形图更改为直线?
我在维基百科上找到了一个关于主食谷物营养成分的数据集。我使用rvest包创建了数据表,并创建了如下所示的图形 有人向我指出,也许最好用一条垂直线而不是一根横杆来表示推荐的饮食异温塞尔达 1如何创建代表推荐饮食量的单独垂直线 用于创建图形的代码如下:我不确定是否应该包含用于收集和整理数据的代码。请让我知道这是否有帮助R 如何将单个值的表示方式从条形图更改为直线?,r,ggplot2,R,Ggplot2,我在维基百科上找到了一个关于主食谷物营养成分的数据集。我使用rvest包创建了数据表,并创建了如下所示的图形 有人向我指出,也许最好用一条垂直线而不是一根横杆来表示推荐的饮食异温塞尔达 1如何创建代表推荐饮食量的单独垂直线 用于创建图形的代码如下:我不确定是否应该包含用于收集和整理数据的代码。请让我知道这是否有帮助 ggplot(grain.nut, aes(grain, nutrients, fill = grain)) + facet_wrap(~ nutrient.component.
ggplot(grain.nut, aes(grain, nutrients, fill = grain)) +
facet_wrap(~ nutrient.component., scales = "free") +
geom_bar(stat = "identity", position = "dodge") +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))
我试过使用geom_vline和geom_hline。但我认为我的问题在于,我试图通过levelsgrain.nut$grain调用RDA的值,其输出是推荐的膳食津贴
geom_vline(aes(xintercept = levels(grain.nut$grain)[1]))
任何帮助都将不胜感激 这里是一种使用geom_linerange或geom_pointrange的方法
首先是数据:
library("rvest")
library(tidyverse)
url <- "https://en.wikipedia.org/wiki/Staple_food"
nutrient <- url %>%
read_html() %>%
html_nodes(xpath='//*[@id="mw-content-text"]/div/table[2]') %>%
html_table()
情节:
ggplot() +
geom_col(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain!="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(grain, value, fill = grain), position = "dodge")+
geom_pointrange(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain=="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(x = grain, ymin = 0, ymax = value, y = value, color = grain), size = 0.3, show.legend = F)+
facet_wrap(~ nutrient, scales = "free") +
scale_x_discrete(limits = lev) +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))
基本上有两个层用于不同的数据:geom_col(数据不含RDA)和geom_pointrange(数据仅含RDA)。并且,为了匹配lev对象,在scale_x_离散中改变顺序
如果您不喜欢这些点,请使用geom_linerange并在aes调用中省略y
或者你是这个意思
ggplot() +
geom_col(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain!="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(grain, value, fill = grain), position = "dodge")+
geom_hline(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain=="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(yintercept = value), show.legend = F)+
facet_wrap(~ nutrient, scales = "free") +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))
您创建的第二个图形正是我正在寻找的解决方案。非常感谢。
ggplot() +
geom_col(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain!="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(grain, value, fill = grain), position = "dodge")+
geom_hline(data = nutrient[[1]] %>%
as.tibble() %>%
gather(grain, value, 2:ncol(.)) %>%
filter(grain=="RDA") %>%
mutate(nutrient = `Nutrient component:`,
value = as.numeric(value)), aes(yintercept = value), show.legend = F)+
facet_wrap(~ nutrient, scales = "free") +
coord_flip() +
labs(title = "Nutrient Content of Major Staple Foods per 100 gram Portion",
caption = "https://en.wikipedia.org/wiki/Staple_food#Nutritional_content") +
theme(plot.title = element_text(size = 30, face = "bold")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.position = c(0.80,0.05), legend.direction = "horizontal") +
theme(legend.title = element_blank()) +
theme(plot.caption = element_text(hjust = 0.84)) +
guides(fill=guide_legend(reverse=TRUE)) +
scale_fill_manual(values = c("#e70000",
"#204bcc",
"#68ca3b",
"#fe9bff",
"#518901",
"#de0890",
"#fcba4c",
"#292c7a",
"#e69067",
"#79b5ff",
"#68272d",
"#c9cb6c"))