我有一个用ggplot制作的图,看起来像这样:

我希望将每个杆件上的数字标签固定/粘贴到x轴上,其中y <= 0。
下面是生成图形的代码:
ggplot(data=df) +
geom_bar(aes(x=row, y=numofpics, fill = crop, group = 1), stat='identity') +
geom_point(data=df, aes(x = df$row, y=df$numofparcels*50, group = 2), alpha = 0.25) +
geom_line(data=df, aes(x = df$row, y=df$numofparcels*50, group = 2), alpha = 0.25) +
geom_text(aes(x=row, y=numofpics, label=bbch)) +
geom_hline(yintercept=300, linetype="dashed", color = "red", size=1) +
scale_y_continuous(sec.axis= sec_axis(~./50, name="Number of Parcels")) +
scale_x_discrete(name = c(),breaks = unique(df$crop), labels = as.character(unique(df$crop)))+
labs(x=c(), y="Number of Pictures")我尝试过使用vjust和position_nudge来处理geom_text元素,但我能找到的每个解决方案都会将geom_text中每个元素的位置分别相对于其当前位置进行更改。因此,我尝试的每一件事都会出现这样的情况:

如何将文本放置在x轴的底部,其中y <=为0,可能还会引入angle = 45
数据帧链接= https://drive.google.com/file/d/1b-5AfBECap3TZjlpLhl1m3v74Lept2em/view?usp=sharing
发布于 2019-04-29 17:53:53
正如我在注释中所说的,只需将文本的y坐标设置为0或更低,并指定角度:geom_text(aes(x=row, y=-100, label=bbch), angle=45)

发布于 2019-04-29 17:50:06
我在一个代理服务器后面,它会阻止与谷歌硬盘的连接,所以我无法访问你的数据。我无法测试这一点,但我会在我的数据集中引入一个新的label字段,如果为y<0,则将y设置为0:
df <- df %>%
mutate(labelField = if_else(numofpics<0, 0, numofpics)然后,我将在geom_text调用中使用此标签字段:
geom_text(aes(x=row, y=labelField, label=bbch), angle = 45)希望这能有所帮助。
发布于 2019-04-29 17:52:43
您可以简单地在geom_text中定义y值(例如-50)
ggplot(data=df) +
geom_bar(aes(x=row, y=numofpics, fill = crop, group = 1), stat='identity') +
geom_point(data=df, aes(x = df$row, y=df$numofparcels*50, group = 2), alpha = 0.25) +
geom_line(data=df, aes(x = df$row, y=df$numofparcels*50, group = 2), alpha = 0.25) +
geom_text(aes(x=row, y=-50, label=bbch)) +
geom_hline(yintercept=300, linetype="dashed", color = "red", size=1) +
scale_y_continuous(sec.axis= sec_axis(~./50, name="Number of Parcels")) +
scale_x_discrete(name = c(),breaks = unique(df$crop), labels = 裁剪(唯一(df$as.character))+实验室(x=c(),y=“图片数量”)
https://stackoverflow.com/questions/55900575
复制相似问题