我有以下数据:
new_pairs
x y Freq start.latittude start.longitude start.station end.latitude
1 359 519 929 40.75188 -73.97770 Pershing\nSquare N 40.75510
2 477 465 5032 40.75514 -73.98658 Broadway &\nW 41 St 40.75641
3 484 519 1246 40.75188 -73.97770 Pershing\nSquare N 40.75500
4 484 318 2654 40.75320 -73.97799 E 43 St &\nVanderbilt\nAve 40.75500
5 492 267 1828 40.75098 -73.98765 Broadway &\nW 36 St 40.75020
6 492 498 957 40.74855 -73.98808 Broadway &\nW 32 St 40.75020
7 492 362 1405 40.75173 -73.98754 Broadway &\nW 37 …Run Code Online (Sandbox Code Playgroud) 我有需要放入堆栈条形图的数据,但是当我添加计数的标签时,一些标签在类别之上,有些在类别之下。我尝试修改 geom_text 函数的位置参数无济于事。
下面是一个可重现的示例,显示了该类别上方“Under”类别座位的标签以及酒吧内“Over”类别座位的标签。
library(tidyverse)
data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE),
DueDate = sample(
seq( as.Date("2015-01-01"),
as.Date("2015-06-30"), by="1 month") ,
6000,replace = TRUE),
stringsAsFactors = TRUE) %>%
group_by(AgeGroup,DueDate) %>%
tally() %>% ungroup %>%
ggplot() +
geom_bar(aes(x=DueDate, y=n, fill = AgeGroup),stat = "identity") +
geom_text(aes(x=DueDate, y=n
,label = prettyNum(n,big.mark = ","))
, vjust = 0, size = 2) +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title="Where are the labels")
Run Code Online (Sandbox Code Playgroud)
我,这是我的问题.
我想用ggplot2创建一个geom_bar,条形图上有标签.
这是一个可重现的例子和生成的图:
prop = c(rep("A",2),rep("B",2),rep("C",2))
n = c(rep(29,2),rep(63,2),rep(25,2))
var = c(rep(c("sortant","entrant"),3))
value = c(10,19,43,20,10,15)
mydata = data.frame(cbind(prop,n,var,value))
> mydata
prop n var value
A 29 sortant 10
A 29 entrant 19
B 63 sortant 43
B 63 entrant 20
C 25 sortant 10
C 25 entrant 15
mydata$n = as.integer(as.character(mydata$n))
mydata$value = as.integer(as.character(mydata$value))
ggplot(data = mydata)+aes(x = prop, y = value, fill = var)+geom_bar(stat = 'identity')+
scale_fill_brewer(palette = 'Paired')+geom_text(aes(label = value))
Run Code Online (Sandbox Code Playgroud)
我想制作这个情节,而不是手动创建"y_pos"(如下面的例子),因为我每周都要用不同的数据制作这个情节.
mydata2 = mydata
mydata2$y_pos = c(7,20,20,50,7,17)
>mydata2 …Run Code Online (Sandbox Code Playgroud) 我创建了一个堆叠的条形图,并使用两个变量对其进行了分面。我无法将值放置在条形图的中间,它要么出现在极端位置要么重叠。预期的图像如下:

我在下面粘贴了脚本。任何帮助,将不胜感激。
library(dplyr)
library(reshape2)
library(ggplot2)
year<-c("2000","2000","2010","2010","2000","2000","2010","2010")
area<-c("Rural","Rural","Rural","Rural","Urban","Urban","Urban","Urban")
sex<-c("Male","Female","Male","Female","Male","Female","Male","Female")
city<-c(58,61,74,65,51,55,81,54)`
village<-c(29,30,20,18,42,40,14,29)
town<-c(13,9,6,17,7,5,5,17)
data<-cbind.data.frame(year,area,sex,city,village,town)
dre<-melt(data,id.vars = c("year","area","sex"))
dre <- arrange(dre,year,area,sex,variable) %>%
mutate(pos = cumsum(value) - (0.5 * value))
a <- ggplot(dre,aes(factor(sex),value,fill=variable)) +
geom_bar(stat='identity',position="stack")
b <- a +
facet_wrap(factor(year)~area)
c <- b +
geom_text(aes(label=paste0(value,"%"),y=pos),
position="stack",size=2,hjust=0.85,color="black")
d <- c +
coord_flip() +
theme_bw() +
scale_fill_brewer()
print(d)
Run Code Online (Sandbox Code Playgroud)