我想在分面饼图上添加数据标签.
也许有人可以帮助我.
我的数据:
year <- c(1,2,1,2,1,2)
prod <- c(1,1,2,2,3,3)
quantity <- c(33,50,33,25,34,25)
df <- data.frame(year, prod, quantity)
rm(year, prod, quantity)
Run Code Online (Sandbox Code Playgroud)
码:
library(ggplot2)
# center's calculated by hand
centr2 <- c(16, 25, 49, 62.5, 81, 87.5)
ggplot(data=df, aes(x=factor(1), y=quantity, fill=factor(prod))) +
geom_bar(stat="identity") +
geom_text(aes(x= factor(1), y=centr2, label = df$quantity), size=10) +
facet_grid(facets = .~year, labeller = label_value) +
coord_polar(theta = "y")
Run Code Online (Sandbox Code Playgroud)
我的结果是:

如果我删除coord_polar(theta ="y"),我将得到以下图:

现在我很清楚,为什么我的数据标签不匹配.
但我不知道如何解决它.
我读到:
1.在饼图上放置标签
2. 在带有刻面密度的ggplot中添加文本
3. 饼图将其文本放在彼此的顶部
但没有找到答案.
我在某处读到有一种方法(在行的开头或末尾有一些字符),这会强制表达式的自动打印结果.
我想自动打印n,即不必键入n后n <- 3
n <- 3
n
Run Code Online (Sandbox Code Playgroud) 我创建了金字塔,如图,我想为图的每一面添加标签(类似于构面标签).
我的数据:
dt <- data.frame(Answer = factor(x = rep(x = c(1:3), times = 2),
labels = c("Yes", "No", "Maybe")),
Gender = factor(x = rep(x = c(1:2), each = 3),
labels = c("Female", "Male")),
Prc = c(74.4, 25.0, 0.6, 61.3, 35.5, 3.2),
label = c("74.4%", "25.0%", "0.6%", "61.3%", "35.5%", "3.2%"))
Run Code Online (Sandbox Code Playgroud)
我的情节:

我的剧情生成代码:
xmi <- -70
xma <- 80
library(ggplot2)
ggplot(data = dt, aes(x = Answer, fill = Gender)) +
geom_bar(stat = "identity", subset = .(Gender == "Female"), aes(y = Prc)) + …Run Code Online (Sandbox Code Playgroud)