Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2中多组钢筋的宽度_R_Ggplot2_Width_Geom Bar - Fatal编程技术网

R ggplot2中多组钢筋的宽度

R ggplot2中多组钢筋的宽度,r,ggplot2,width,geom-bar,R,Ggplot2,Width,Geom Bar,我试图复制罗曼在这篇文章中给出的答案: 但我无法解决我的问题。当条形宽度相同时,组之间的距离太大。使用facet\u网格时也会出现同样的问题 我的df: df <- structure(list(discipline = structure(c(2L, 3L, 3L, 2L, 2L, 2L, 4L, 6L, 7L, 3L, 4L, 6L, 8L, 8L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("", "Biogeochemi

我试图复制罗曼在这篇文章中给出的答案: 但我无法解决我的问题。当条形宽度相同时,组之间的距离太大。使用
facet\u网格时也会出现同样的问题

我的df:

df <- structure(list(discipline = structure(c(2L, 3L, 3L, 2L, 2L, 2L,  4L, 6L, 7L, 3L, 4L, 6L, 8L, 8L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("", "Biogeochemistry", "Ecology", "Geochemistry", "Geography", "Management",  "Microbiology", "Oceanography"), class = "factor"), focus = structure(c(34L, 55L, 40L, 47L, 54L, 57L, 47L, 19L, 31L, 25L, 23L, 25L, 47L, 52L,13L, 20L, 23L, 16L, 26L, 27L), .Label = c("", "Abiotic measures", "Acidification", "Biogeochemichal budgets", "Biogeochemistry",  "Biogeochemistry, discharge", "Blue Carbon", "Chromophoric Dissolved organic matter, river plume", "Coastal anthromes", "Connectivity", "Coral reefs", "Ecology", "Ecosystem Function", "Ecosystem Services", "Embryo plants", "Fisheries", "Food webs", "Global change", "Governance", "Groundwater", "Hidrology", "Integrative Magamenet", "Isotopes", "Land-sea interactions","Land-sea interface", "Land use", "Life history", "Life traits", "Livelihoods", "Management", "Microbial community", "Modelling water quality",  "Nitrogen fluxes", "Nutrients", "Parasites", "ph, CO2", "Planning", "Pollutants", "Pollution", "Primary production", "Remote Sensing", "Resilience", "resilience, self-organization", "Restoration", 
"Salinization", "Sea level rise", "Sediment flux", "Sediments", "socio land-sea interactions", "Species interaction", "Submarine ground water", "Submarine groundwater", "Subsidies", "Trace metals", "Trophic interactions",  "Water quality", "Water resources"), class = "factor"), n = c(39L, 17L, 11L, 9L, 6L, 5L, 5L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 2L, 2L,  2L, 2L, 2L, 2L)), row.names = c(NA, -20L), class = c("tbl_df","tbl", "data.frame"))

然后我使用了平面网格

ggplot(df, aes(x = (discipline), y = n, fill = reorder(focus, n))) + 
  geom_bar(position = "dodge", stat = "identity") + ylab("N") + theme_classic() + geom_text(aes(label=focus), position = position_dodge2(width = 0.9, preserve = "single"), angle = 90, hjust = -0.1) + theme(legend.position = "none") +  facet_grid(scales = "free_x", space = "free_x", switch = "x")
即使钢筋宽度相等,组之间的距离也太大。
我能做些什么来解决这个问题?

也许可以试试这个。问题似乎出在
位置上。如果为条形定义
position\u dodge2()
,则可以避免使用大条形。代码如下:

library(ggplot2)
#Code
ggplot(df, aes(x = (discipline), y = n, fill = reorder(focus, n))) + 
  geom_bar(position = position_dodge2(0.9,preserve = 'single'),
           stat = "identity") + ylab("N") +
  theme_classic() + 
  geom_text(aes(label=focus), position = position_dodge2(width = 0.9, preserve = "single"),
            angle = 90, hjust = -0.1) + theme(legend.position = "none") +
  facet_grid(scales = "free_x", space = "free_x", switch = "x")
输出:

然而,原始代码会产生以下结果(使用
position=“dodge”
):


也许可以试试这个。问题似乎出在
位置上。如果为条形定义
position\u dodge2()
,则可以避免使用大条形。代码如下:

library(ggplot2)
#Code
ggplot(df, aes(x = (discipline), y = n, fill = reorder(focus, n))) + 
  geom_bar(position = position_dodge2(0.9,preserve = 'single'),
           stat = "identity") + ylab("N") +
  theme_classic() + 
  geom_text(aes(label=focus), position = position_dodge2(width = 0.9, preserve = "single"),
            angle = 90, hjust = -0.1) + theme(legend.position = "none") +
  facet_grid(scales = "free_x", space = "free_x", switch = "x")
输出:

然而,原始代码会产生以下结果(使用
position=“dodge”
):


这是我的问题。我希望规程的距离与图2相同,但具有相同的宽度条。例如,在第一幅图中,微生物学和海洋学之间的距离太大了,我想这是我的问题。我希望规程的距离与图2相同,但具有相同的宽度条。例如,在图1中,我认为微生物学和海洋学之间的距离太大了