如果这是一个非常微不足道的问题,我很抱歉,但我已经在这个问题上花了几个小时了。我有一个带有面网格的 ggplot,其中包含几个因素。问题是分面的标题不可读。有“太多”。
facet_grid(years.since.peace ~ type_of_termination+strategy.cm6.YP)
我知道我可以更改面板标题的字体大小,或者将其分成几个不同的图表。但我感兴趣的是扩大绘图区域,以便有足够的空间容纳面板/方面的所有标题?该图很可能最终会成为海报,因此将其打印在例如 A2 中就可以了。
我检查了空间、缩放和缩小选项 - 但似乎它们并不适合我想要做的事情。我还尝试修改 pdf() 命令的宽度和高度选项。但图表只是变得更大,而没有解决分面标题不可读的问题。
也许我只是错了我想做的事?

我创建了一个堆积条形图,描绘了市政当局在几年内(= x轴)的议会席位(= y轴)的分布.使用的代码和一些数据如下.不幸的是,我没有足够的积分来发布图表.
不同的政党也与称为"意识形态"的变量相关联,作为不同政治取向的类别("进步","中等","保守").
我希望以这样的方式修改颜色,例如所有保守派派对都有不同的蓝调; 所有进步党派各种绿色; 和所有温和派对,例如不同种类的红色;
意识形态的变量在同一个数据帧(y)中.
任何提示如何进行此修改?我已经尝试过color = factor(意识形态)和group =意识形态,但无济于事.我也知道这个相关的条目在ggplot中使用预定义的调色板,但它并不特别与我的问题有关.
非常感谢.
使用命令:
municipality.plot <- ggplot(y, aes(x=as.factor(year), y=seats, fill=party, color=party)) +
geom_bar(bandwidth=1, stat="identity", group="party", position="fill") +
labs(x="year", y="% of seats for municipality")
Run Code Online (Sandbox Code Playgroud)
样本数据:
year district.id party seats ideology
1 2012 127 Stranka Pravde I Razvoja Bosne I Hercegovine 1 p
2 2012 127 Savez Za Bolju Buducnost (SBB) 3 p
3 2008 127 Stranka Demokratske Akcije (SDA) 13 p
4 2004 127 Stranka Demokratske Akcije (SDA) 14 …Run Code Online (Sandbox Code Playgroud) 是否有任何功能等可避免散点图中相同数据点的重叠数据标签?我已经检查了对textxy,direct.label和geom_text()的各种问题/回答,但我没有成功.也许这根本不可能.
以下是相关数据的示例:
structure(list(cowc = structure(c(5L, 7L, 24L, 24L, 23L, 36L,
34L, 38L, 23L, 6L, 8L, 38L, 38L, 23L, 5L, 7L, 24L, 24L, 23L,
36L, 34L, 38L, 23L, 6L, 8L, 38L, 38L, 23L), .Label = c("AFG",
"ANG", "AZE", "BNG", "BOS", "BUI", "CAM", "CDI", "CHA", "COL",
"CRO", "DOM", "DRC", "ETH", "GNB", "GRG", "GUA", "IND", "INS",
"IRQ", "KEN", "LAO", "LBR", "LEB", "MAL", "MLD", "MZM", "NEP",
"NIC", "PHI", "PNG", "RUS", "RWA", "SAF", "SAL", "SIE", "SOM",
"SUD", "TAJ", "UKG", "YAR", "ZIM"), class = "factor"), conflict …Run Code Online (Sandbox Code Playgroud) 我有一个带有分面网格的条形图,我想添加每个子图中存储在单独数据框中的观察数。
条形图是用
bar.plot <- ggplot(BarDiff.m.s, aes(x=value.change, fill=incompatibility))+
geom_bar(binwidth=1)+
labs(x="score differences", y="count / years since start of PSA")+
geom_vline(aes(xintercept=0), linetype="dotted")+
theme(plot.title=element_text(face="bold", size=10),
legend.position= "bottom")+
scale_fill_brewer(palette="Set1")+
facet_grid(years.since.peace ~ strategy.cm6.YP, space="free")
Run Code Online (Sandbox Code Playgroud)
我尝试通过在 geom_bar 行之后添加来添加 geom_text
geom_text(data=num.obs, aes(label=paste("obs=",num.obs),y=4,x=min(BarDiff.m.s$value.change)))
Run Code Online (Sandbox Code Playgroud)
但是,我收到了错误消息
Error in eval(expr, envir, enclos) : object 'incompatibility' not found
Run Code Online (Sandbox Code Playgroud)
显然,出于某种原因,我必须考虑 geom_text 中的“填充”变量;我试图将 group=BarDiff.ms$incompatibility 添加到 geom_text,但无济于事。
我已经看到如何在 R 中使用 ggplot/geom_bar 从条形顶部的数据集中添加自定义标签?,但如果可能的话,我想将两个 data.frames 分开并了解如何解决“填充”问题。非常欢迎任何建议!谢谢。

该图的相关数据是
BarDiff.m.s <- structure(list(value.change = c(-1, -1, -2, -2, 1, NA, 0, -2,
-1, -2, NA, 2, -3, NA, NA, …Run Code Online (Sandbox Code Playgroud) 使用下面的代码,刻面 bB 中的标签未正确定位。问题似乎源于这样一个事实,即没有position_dodge(preserve="single")for geom_text(对吗?)。我知道我可以“手动”添加一个空的虚拟单元格(在 facet bB 中填充 y=0),但我想知道是否有任何方法可以直接在 ggplot 中纠正它?
v1 <- LETTERS[1:2]
v2 <- letters[1:2]
v3 <- c("x","y")
g <- expand.grid(v1,v2,v3)
val=c(sample(10,8))
df<- data.frame(g,val)
df<- df[-8,]
df %>% ggplot() +
geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3),
stat="identity",
position=position_dodge(preserve="single"))+
geom_text(aes(x=Var2, y=val+1, label=val, group=Var3),
position=position_dodge(width=1))+
facet_grid(Var1~Var2, scale="free_x")
Run Code Online (Sandbox Code Playgroud)
更新/答案:使用 position_dodge2 将条形与标签对齐(但是,条形居中且未与其他方面中的条形对齐)。
df %>% ggplot() +
geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3), stat="identity",
position=position_dodge2(preserve="single"))+
geom_text(aes(x=Var2, y=val+1, label=val, group=Var3),
position=position_dodge2(width=1))+
facet_grid(Var1~Var2, scale="free_x")
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用创建热图ggplots geom_tile。目前,网格线以每个网格线的中间为中心geom_tile。我想要的是网格线与每个图块的开始/结束对齐。我看过一些相关的帖子(此处,此处,但所有帖子都在处理连续尺度。就我而言,两个尺度都是离散/因子。有什么想法吗?非常感谢!
library(tidyverse)
my_iris <- iris %>%
mutate(sepal_interval=cut(Sepal.Length, 4)) %>%
group_by(sepal_interval, Species)
my_iris %>%
summarise(n_obs=n()) %>%
ggplot()+
geom_tile(aes(y=Species,
x=sepal_interval,
fill=n_obs))+
theme_bw()+
theme(panel.grid = element_line(color="black"))
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-01-28 创建
我想创建一个图,其中轴标题位于顶部/左侧,轴标签位于底部/右侧。这可以通过创建一个重复的轴并删除不需要它们的边上的轴标题和标签(通过主题选项)来完成。
然而,在将 x 轴标题放在多面图中的顶部时,我遇到了一个问题。出于某种原因,尽管使用了选项,但刻面的条形文本仍位于轴标题下方strip.placement="outside"。有趣的是,如果我不隐藏轴标签(见下图),它们在条形文本下方,但轴标题在条形文本上方。
知道如何将轴标题也移动到 strip.text 下方吗?请注意,我正在使用 ggplot ggplot2_3.3.0.9000。非常感谢。
library(tidyverse)
iris %>%
ggplot()+
geom_bar(aes(x=Sepal.Length,
y=Sepal.Width),
stat="identity")+
facet_wrap(~Species,
strip.position = "top")+
scale_y_continuous(sec.axis = dup_axis())+ #create secondary axis
scale_x_continuous(sec.axis = dup_axis())+ #create secondary
theme(strip.placement = "outside",
axis.title.x.top = element_text(hjust=0), #left align axis title on top
axis.title.x.bottom = element_blank(), #remove title on x axis/bottom
axis.ticks.length.x.top = unit(0, units="cm"), #remove axis ticks on top
axis.text.y.left = element_blank(), #remove axis label on left side
axis.title.y.right = element_blank(), #remove axis title on right side …Run Code Online (Sandbox Code Playgroud) 在“一次性”过滤基础数据框后,如何将变量的值分配给 ggplot 标题。
library(tidyverse)
#THIS WORKS
d <- mtcars %>%
filter(carb==4)
d %>%
ggplot()+
labs(title=paste(unique(d$carb)))+
geom_bar(aes(x=am,
fill=gear),
stat="count")
Run Code Online (Sandbox Code Playgroud)

#THIS DOESN'T WORK
mtcars %>%
filter(carb==4) %>%
ggplot()+
labs(title=paste(data=. %>% distinct(carb) %>% pull()))+
geom_bar(aes(x=am,
fill=gear),
stat="count")
#> Error in as.vector(x, "character"): cannot coerce type 'closure' to vector of type 'character'
#THIS ALSO DOESN'T WORK
mtcars %>%
filter(carb==3) %>%
ggplot()+
labs(title=paste(.$carb))+
geom_bar(aes(x=am,
fill=gear),
stat="count")
#> Error in paste(.$carb): object '.' not found
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2020 年 4 月 23 日创建
我正在处理一些原始文本,并希望将所有多个空格替换为一个空格。通常,会使用 stringr's str_squish,但不幸的是它也删除了我必须保留的换行符(\n 和 \r)。
任何想法?以下是我的尝试。非常感谢!
library(tidyverse)
x <- "hello \n\r how are you \n\r all good?"
str_squish(x)
#> [1] "hello how are you all good?"
str_replace_all(x, "[:space:]+", " ")
#> [1] "hello how are you all good?"
str_replace_all(x, "\\s+", " ")
#> [1] "hello how are you all good?"
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2020-07-01 创建
假设我有一个包含命名元素的列表。元素是小标题。\n我想将列表转换为一个小标题。
\npurrr 的list_rbind工作原理,但有一个严重的缺点(至少对于我的目的而言),它会删除 0 行 tibble 元素(尽管它是一个命名元素)。
library(tidyverse)\n\nli <- list("a"=tibble(x=1), "b"=tibble(x=2), "c"=tibble(x=NULL))\nli\n#> $a\n#> # A tibble: 1 \xc3\x97 1\n#> x\n#> <dbl>\n#> 1 1\n#> \n#> $b\n#> # A tibble: 1 \xc3\x97 1\n#> x\n#> <dbl>\n#> 1 2\n#> \n#> $c\n#> # A tibble: 0 \xc3\x97 0\n\n\nli %>% purrr::list_rbind(., names_to="element_names")\n\n#> # A tibble: 2 \xc3\x97 2\n#> element_names x\n#> <chr> <dbl>\n#> 1 a 1\n#> 2 b 2\nRun Code Online (Sandbox Code Playgroud)\n我正在寻找的结果是
\n#> element_names x\n#> <chr> <dbl>\n#> 1 a 1\n#> 2 b 2\n#> …Run Code Online (Sandbox Code Playgroud) 我正在尝试提取嵌套列表的最后一个元素。我怎样才能做到这一点purrr?在下面的示例中,结果应该是所有值为“c”的元素。我已经看到了这个,但我对 purrr 的解决方案特别感兴趣。
非常感谢
x <- list(list(1,2,"c"), list(3,"c"), list("c"))
x
#> [[1]]
#> [[1]][[1]]
#> [1] 1
#>
#> [[1]][[2]]
#> [1] 2
#>
#> [[1]][[3]]
#> [1] "c"
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] 3
#>
#> [[2]][[2]]
#> [1] "c"
#>
#>
#> [[3]]
#> [[3]][[1]]
#> [1] "c"
map(x, purrr::pluck, tail)
#> Error in map(x, purrr::pluck, tail): could not find function "map"
map(x, purrr::pluck, length(x))
#> Error in map(x, purrr::pluck, length(x)): …Run Code Online (Sandbox Code Playgroud)