Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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 将标签与ggplot中的方框图对齐_R_Ggplot2_Label - Fatal编程技术网

R 将标签与ggplot中的方框图对齐

R 将标签与ggplot中的方框图对齐,r,ggplot2,label,R,Ggplot2,Label,我有多个箱线图,我希望用上面的实际值来标记它们。然而,我似乎无法使它们对齐。我最终得到了如下这样的结果:标签没有对齐 我已经试着修补一下了 geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-1) 但是没有用。如有任何建议,将不胜感激 我的脚本如下: library(reshape2) library(tidyverse) library(dplyr) library(ggplot2) library

我有多个箱线图,我希望用上面的实际值来标记它们。然而,我似乎无法使它们对齐。我最终得到了如下这样的结果:标签没有对齐

我已经试着修补一下了

geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-1) 
但是没有用。如有任何建议,将不胜感激

我的脚本如下:

library(reshape2)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(stringr)

barCount <- tibble::tribble(
  ~Year, ~Lateral.bar, ~Bar.accreted.to.island, ~Mid.channel.bar,
  1990,          105,                     134,               62,
  2010,          102,                     189,              102
)


df2 <- melt(barCount, id="Year") %>% 
  mutate(
    Year = Year %>% as.factor(),
    variable = variable %>% str_replace_all("\\.", " ")
  )

barPlot <- ggplot(df2, aes(Year,value)) +
  geom_bar(aes(fill=variable),position="dodge",stat="identity") +
  labs(y="Quantity",fill="")+
  scale_fill_manual("Legend",
                    values=c("Lateral bar"="khaki4", "Bar accreted to island"="Khaki2", 
                             "Mid channel bar"="honeydew4"))+
  geom_text(aes(label=value), position=position_dodge(width=0.9),vjust=-1) 


#modifying axis 
barPlot <- barPlot + theme(
  axis.title.x = element_blank(),
  axis.title.y = element_text(size=14),
  axis.text.x = element_text(size=14),
  legend.position="bottom" 

)

barPlot
library(重塑2)
图书馆(tidyverse)
图书馆(dplyr)
图书馆(GG2)
图书馆(stringr)
barCount%as.factor(),
变量=变量%>%str\u替换所有(“\\.”,“”)
)

barPlot干杯,将
fill=variable
从几何图形栏更改为
ggplot()
这样调用(并通过
hjust=.25调整水平方向):

barPlot <- ggplot(df2, aes(Year,value, fill= variable)) +
  geom_bar(position="dodge",stat="identity") +
  labs(y="Quantity",fill="")+
  scale_fill_manual("Legend",values=c("Lateral bar"="khaki4","Bar accreted to island"="Khaki2","Mid channel bar"="honeydew4"))+
  geom_text(aes(label=value),position=position_dodge(width=0.9),vjust=-1, hjust= .25) 

barPlot感谢您抽出时间回答我的问题:)您能向我解释一下为什么只有当fill=变量进入ggplot时才会出现这种情况吗?因为我尝试将它与ggplot和geom_栏一起使用,但它仅对前者有效。@catlovingtaco之所以有效,是因为
geom_text()
现在继承了
ggplot()
中的
fill=variable
,并将其识别为分组变量,以避开其自身的位置。类似地,如果您将
fill=variable
放在
geom_bar()
中,并在
ggplot()
中添加
group=variable
,您的原始代码也会起作用。