我正在尝试使用ggplot生成一个刻面的饼图,并面临在每个切片中间放置文本的问题:
dat = read.table(text = "Channel Volume Cnt
AGENT high 8344
AGENT medium 5448
AGENT low 23823
KIOSK high 19275
KIOSK medium 13554
KIOSK low 38293", header=TRUE)
vis = ggplot(data=dat, aes(x=factor(1), y=Cnt, fill=Volume)) +
geom_bar(stat="identity", position="fill") +
coord_polar(theta="y") +
facet_grid(Channel~.) +
geom_text(aes(x=factor(1), y=Cnt, label=Cnt, ymax=Cnt),
position=position_fill(width=1))
Run Code Online (Sandbox Code Playgroud)
输出:

geom_text应该调整哪些参数才能将数字标签放在饼图切片的中间?
相关问题是Pie plot将其文本放在彼此的顶部,但它不处理facet的情况.
更新:在上面的问题中遵循Paul Hiemstra的建议和方法我改变了代码如下:
---> pie_text = dat$Cnt/2 + c(0,cumsum(dat$Cnt)[-length(dat$Cnt)])
vis = ggplot(data=dat, aes(x=factor(1), y=Cnt, fill=Volume)) +
geom_bar(stat="identity", position="fill") +
coord_polar(theta="y") +
facet_grid(Channel~.) +
geom_text(aes(x=factor(1),
---> y=pie_text,
label=Cnt, ymax=Cnt), …Run Code Online (Sandbox Code Playgroud) 只是试图修复这个重叠的标签:

我的代码:
values=c(164241,179670)
labels=c("Private", "Public")
colors=c("#cccccc", "#aaaaaa")
categoriesName="Access"
percent_str <- paste(round(graph$values / sum(graph$values) * 100,1), "%", sep="")
values <- data.frame(val = graph$values, Type = graph$labels, percent=percent_str )
pie <- ggplot(values, aes(x = "", y = val, fill = Type)) + geom_bar(width = 1) +
geom_text(aes(y = **val + 1**, **hjust=0.5**, **vjust=-0.5**, label = percent), colour="#333333", face="bold", size=10) +
coord_polar(theta = "y") + ylab(NULL) + xlab(NULL) +
scale_fill_manual(values = graph$colors) + labs(fill = graph$categoriesName) +
opts( title = graph$title,
axis.text.x = …Run Code Online (Sandbox Code Playgroud) 我使用聚合(,, FUN = sum)函数构造了以下数据:
structure(list(Group.1 = structure(c(1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L), .Label = c("Black or African American", "White Alone",
"White Alone LR"), class = "factor"), Group.2 = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("bus", "mixed", "rail"
), class = "factor"), x = c(75143.5182835844, 198737.537113379,
46973.6469041183, 46199.2335265697, 128026.568239224, 28933.3028730992,
75876.5845180076, 495166.957025367, 5909.04640985574), pos = c(37571.7591417922,
99368.7685566897, 23486.8234520592, 98243.1350468693, 262750.821232991,
61440.2983406679, 159281.044069158, 574347.583865287, 78861.4729821454
), labe = c(" 75,144", "198,738", " 46,974", " …Run Code Online (Sandbox Code Playgroud) 我的数据框看起来像
df
Group value
1 Positive 52
2 Negative 239
3 Neutral 9
Run Code Online (Sandbox Code Playgroud)
我想使用ggplot制作数据框的饼图.
pie <- ggplot(df, aes(x="", y=value, fill=Group)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0)
Run Code Online (Sandbox Code Playgroud)
这是我的饼图.
但是当我尝试在图表上添加百分比标签时
pie <- ggplot(df, aes(x="", y=value, fill=Group)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) +
geom_text(aes(y = value/2 + c(0, cumsum(value)[-length(value)]),
label = percent(value/300 )), size=5)
Run Code Online (Sandbox Code Playgroud)
这是我的结果.
我已经看到了许多与我相同的问题,即 R + ggplot2 =>在facet饼图上添加标签,解决方案没有帮助.
我正在尝试将标签与我的饼图与ggplot2匹配:
码:
values=c(59,4,4,11,26)
labels=c("catA", "catB","catC","catD","catE")
pos = cumsum(values)- values/2
graph <- data.frame(values, labels,pos)
categoriesName="Access"
percent_str <- paste(round(graph$values / sum(graph$values) * 100,1), "%", sep="")
values <- data.frame(val = graph$values, Type = graph$labels, percent=percent_str, pos = graph$pos )
pie <- ggplot(values, aes(x = "", y = val, fill = Type)) +
geom_bar(width = 1,stat="identity") +
geom_text(aes(x= "", y=pos, label = val), size=3)
pie + coord_polar(theta = "y")
Run Code Online (Sandbox Code Playgroud)
我读了这些主题,但没有成功:
我正在尝试用ggplot2绘制一个饼图.我的代码如下所示.
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
ggplot(df, aes(x = "", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart")
Run Code Online (Sandbox Code Playgroud)
我想显示百分比值.我怎样才能做到这一点?