ggplot的多面图错误。尝试使用多面ggplot协调y值

所以我尝试在多面ggplot上编辑y值,因为在编织时我在绘图上遇到了一些不准确之处。我对R和R Markdown非常陌生,因此我无法弄清楚为什么,例如,当美国的GDP PPP的美元金额最高时,它的门槛就会低于其他值。其他值也存在一些差异,当这些值之间仅相差$ 100- $ 500时,柱线看起来要高得多。有谁知道可能是什么问题?这是我的代码:

library(ggplot2)
library(tidyr)

OECD_IMF <- read.csv("OECD_IMF.csv")
attach(OECD_IMF)
detach(OECD_IMF)

OECD_IMFlong <- gather(OECD_IMF,key="measure",value="value",c("GDP_Nom_T","GDP_PPP","Aid_Perc"))

variable_names <- list("GDP_Nom_T" = "GDP Per Capita 2018 (USD Thousands)","GDP_PPP" = "GDP Purchasing Power Parity 2018 (USD Trillions)","Aid_Perc" = "Average % of Gross National Income Given in Foreign Aid (2000-2016)")

variable_labeller <- function(variable,value){
  return(variable_names[value])}

ggplot(OECD_IMFlong,aes(x=Country,y=value,fill=Country))+
geom_bar(stat='identity')+ geom_text(aes(label=value),vjust=1.6,color="white",size=2.1)+ 
facet_wrap(~measure,scales="free_y",ncol=1,labeller= variable_labeller)+ 
labs(caption="Source: Official Development Assistance Database & IMF World Economic Outlook")+ ylab("Value")+ 
theme_grey() + theme(axis.text.x = element_text(angle = 45,hjust = 1),axis.title.y=element_blank(),axis.title.x=element_blank())

ggplot的多面图错误。尝试使用多面ggplot协调y值

yqf1996 回答:ggplot的多面图错误。尝试使用多面ggplot协调y值

正如@StupidWolf所指出的那样,问题在于您的value列是字符而不是数字向量。因此,在绘制之前必须将value转换为数字。为了获得正确的值和标签,我添加了一个新的变量value1,它是给geom_bar的数字,而value仍然是字符,并给了geom_text以便用漂亮的标签标记条形$标签。试试这个

library(ggplot2)
library(stringr)
library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter,lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect,setdiff,setequal,union

OECD_IMFlong <- structure(list(Country = structure(c(7L,2L,5L,1L,6L,3L,4L,7L,4L),.Label = c("Canada","France","Germany","Italy","Japan","UK","USA"),class = "factor"),GDP_Nom = c(6.7063e-05,4.4062e-05,4.345e-05,4.845e-05,4.3118e-05,4.9617e-05,3.4575e-05,6.7063e-05,3.4575e-05),measure = c("GDP_Nom_T","GDP_Nom_T","GDP_PPP","Aid_Perc","Aid_Perc"),value = c("$67,063.00","$44,062.00","$43,450.00","$48,553.00",118.00","$49,617.00","$34,575.00","$20.54","$2.78","$4.97","$1.71","$2.86","$3.95","$2.08","0.1717","0.0467","0.2108","0.2843","0.4963","0.374","0.1869")),row.names = c(NA,-21L),class = "data.frame")


variable_names <- list("GDP_Nom_T" = "GDP Per Capita 2018 (USD Thousands)","GDP_PPP" = "GDP Purchasing Power Parity 2018 (USD Trillions)","Aid_Perc" = "Average % of Gross National Income Given in Foreign Aid (2000-2016)")

variable_labeller <- function(variable,value){
  return(variable_names[value])}


OECD_IMFlong_tidy <- OECD_IMFlong %>% 
  mutate(value1 = str_replace_all(value,"(,|\\$)","")) %>% 
  mutate(value1 = as.numeric(value1))


ggplot(OECD_IMFlong_tidy,aes(x=Country,y=value1,fill=Country))+
  geom_bar(stat='identity')+ 
  geom_text(aes(label=value),vjust=1.6,color="white",size=2.1)+ 
  facet_wrap(~measure,scales="free_y",ncol=1,labeller= variable_labeller)+ 
  labs(caption="Source: Official Development Assistance Database & IMF World Economic Outlook") + 
  theme_grey() + 
  theme(axis.text.x = element_text(angle = 45,hjust = 1),axis.text.y = element_blank(),axis.title.y = element_blank(),axis.title.x = element_blank())
#> Warning: The labeller API has been updated. Labellers taking `variable` and
#> `value` arguments are now deprecated. See labellers documentation.

reprex package(v0.3.0)于2020-03-16创建

本文链接:https://www.f2er.com/2641660.html

大家都在问