我需要更改数字格式geom_text()
以包含逗号。
我已经看到了相关的问题,但我无法让这些解决方案发挥作用。我尝试过“sep =”,count/sum(count) 类型,以及我刚刚转录的一些其他代码,但不知道什么意思。在这让我发疯之前,我需要一条生命线。
这是我的数据:
N_PASSENGERS Count Mean_Dist Mean_Time Mean_Fare
<int> <int> <dbl> <dbl> <dbl>
1 1 57216 2.16 10.2 145.
2 2 8421 1.92 9.21 213.
3 3 2022 2.01 9.67 234.
4 4 572 1.96 9.22 351.
5 5 306 2.40 9.84 505.
6 6 184 1.90 7.63 446.
Run Code Online (Sandbox Code Playgroud)
ggplot(Difference, aes(x = N_PASSENGERS, y = Mean_Dist, size = Count)) +
geom_point() +
scale_size(range = c(0, 20)) +
xlim(0, 6) +
ylim(1.75, 2.5) +
geom_text(aes(label = Count), …
Run Code Online (Sandbox Code Playgroud) 我想使用 geom_text_repel 使我的标签尽可能靠近饼图的边缘,除非百分比低于某个值,在这种情况下,标签应该被推得更远并用一条线连接。我从ggplot2 饼图中的移动标签中调整了一个解决方案,但增加了高于阈值的组的 xpos 值。
library(dplyr)
library(ggplot2)
library(ggrepel)
library(scales)
threshold = 0.05
age <- data.frame(Age = c("20 - 29", "30 - 39", "40 - 49", "50 - 59", "60 - 69"), count = c(27, 29, 26, 16, 2))
age <- age %>% mutate(percent = count/sum(count),
cs = rev(cumsum(rev(percent))),
ypos = percent/2 + lead(cs, 1),
ypos = ifelse(is.na(ypos), percent/2, ypos),
xpos = ifelse(percent > threshold, 1.8, 1.3),
xn = ifelse(percent > threshold, 0, 0.5))
ggplot(age, aes_string(x = 1, …
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) 我正在尝试在 geom_box 图上添加极值(异常值与否)的标签。我发现了这个问题,它几乎与我的 [ geom_boxplot 中的极值标签 ggplot2 ]完全相似yonicd提供的答案几乎对我有用:
df=rbind(data.frame(id=rep("1",100),var=paste0("V",seq(1,100)),
val=rnorm(100,0,5)),
data.frame(id=rep("2",100),var=paste0("V",seq(1,100)),
val=rnorm(100,0,3)))
df_bound=df%.%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$out))
df_bound=left_join(df_bound, df, by=c("id","val"))
ggplot(df,aes(x=id, y=val, fill=id, label=var)) + geom_boxplot() +
geom_point(aes(group=id), data=df_bound)+
geom_text(aes(group=id), data=df_bound, hjust=-1, size=4)
Run Code Online (Sandbox Code Playgroud)
很明显,我只需要将 [ $out] 替换为
df_bound=df%.%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$out))
Run Code Online (Sandbox Code Playgroud)
有极值而不是异常值。如果我使用
df_bound=df%.%group_by(id)%>%do(.,data.frame(val=boxplot.stats(.$val)$stats))
Run Code Online (Sandbox Code Playgroud)
异常值的标签不会出现。如果能解决怎么办?
我正在使用 Rshiny 的绘图来创建带有文本标签的散点图。下面是一个可重现的示例:
library(ggplot2)
library(plotly)
dat <- data.frame(LongExpressionValue = rnorm(1:100),
LongMethylationValue = rnorm(1:100),
LongCopyNumberValue = rnorm(1:100))
rownames(dat) <- paste0('n',seq(1:100))
# ggplot
p <- ggplot(data = dat, aes(x = LongExpressionValue, y = LongMethylationValue)) +
geom_point(size = 2) + geom_smooth(method = lm) +
geom_text(aes(label = rownames(dat)), vjust=-1.5, size = 3)
# ggplotly
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
这会创建一个如下图:
如何调整 geom_text 选项以使标签出现在上方并且不与点重叠?我确实想保留我的 ggplot 代码,以便跨应用程序使用它。
谢谢!
我有一个数据集,我有兴趣查看测试分数和经历事件的人的百分比:
dat <- data.frame(score = 1:7,
n.event = c(263,5177,3599,21399,16228,10345,1452),
n.total = c(877,15725,13453,51226,32147,26393,7875),
percentage = c(30,33,27,42,50,39,18))
Run Code Online (Sandbox Code Playgroud)
我可以用图表上的百分比来绘制它,如下所示:
ggplot(data=dat, aes(x=score, y=percentage)) +
geom_line() +
geom_text(aes(label = paste0(dat$percentage,"%")))
Run Code Online (Sandbox Code Playgroud)
或者我可以用这样的分数绘制它:
ggplot(data=dat, aes(x=score, y=percentage)) +
geom_line() +
geom_text(aes(label = paste0("frac(",dat$n.event, ",", dat$n.total,
")")),parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
但我想把它们并排放置。这不起作用:
ggplot(data=dat, aes(x=score, y=percentage)) +
geom_line() +
geom_text(aes(label = paste0(dat$percentage,"%","frac(",dat$n.event,
",", dat$n.total, ")")),parse = TRUE)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
解析错误(text = as.character(lab)) : :1:3: 意外输入 1: 30%frac(263,877) ^
感谢您的帮助!
如何避免 ggplot2 中的这两层重叠?我尝试显示文本,以便它们不会位于点上方。
check_overlap
在避免文本与自身重叠但不与其他图层重叠方面做得很好。
我也尝试过 library geom_text_repel
,但是这个 library 不支持check_overlap
并显示每个数据点的文本。
但我不需要每个点都有文字,就像check_overlap
那样。
ggplot(dat, aes(x = CPI, y = HDI)) +
geom_point(aes(color = Region), shape=21, size=4, position = "identity") +
geom_text(data = dat, aes(label = Country), size=4, check_overlap = TRUE)
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个 geom_point,其中文本标签既排斥又指向它们的关联点,即使我使用的是 position=dodge 或 position=jitter。我也有很多要点要标注,这就是为什么我想使用 ggrepel 或类似的东西。我的理解是我不能使用 ggrepel 的位置参数。
有什么办法可以得到这样的图,除了段指向它们的关联点?
require(ggplot2)
require(ggrepel)
data("mtcars")
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$am <- as.factor(mtcars$am)
require(ggplot2)
require(ggrepel)
dodge = position_dodge(1)
ggplot(mtcars, aes(x = am, y=mpg)) +
geom_point(size=3, position=dodge, alpha=0.5, aes(color=cyl)) +
geom_text_repel(data = mtcars,
aes(label = mpg, x=am, y=mpg), alpha=0.9, size=4,
segment.size = .25, segment.alpha = .8, force = 1)
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) 我试图用希腊符号 kappa 标记 geom_tile 中的瓷砖,如下所示
kappa = 价值
我试过使用 expression() 和 bquote() 但找不到让它工作的方法
df = data.frame(x = letters[1:10],y = letters[11:20], value = 1:10)
p = ggplot(df, aes(x = x, y = y)) +
geom_tile()+geom_text(aes(label= paste("k = ",value,"")), color = "white")
p
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/5bDLv.png
Run Code Online (Sandbox Code Playgroud)