我想在分面饼图上添加数据标签.
也许有人可以帮助我.
我的数据:
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. 饼图将其文本放在彼此的顶部
但没有找到答案.
另一个有趣的ggplot2挑战!我正在尝试创建一个ggsubplot调用,在世界地图上提供饼图.艰难的事情似乎是ggplot2中的饼图是带有极坐标的堆积条形图,并且coord_polar的添加不仅影响子图geom而且影响整个地图本身.有没有人知道如何仅将某个坐标方案应用于子图调用的一部分?这是我到目前为止:
library(ggplot2)
library(maps)
library(mapproj)
#install.packages("devtools")
library(devtools)
# install ggsubplot from github repo, not currently on CRAN
install_github(username="garrettgman", repo="ggsubplot")
world = map_data("world")
loc_pie = structure(list(Region = structure(c(3L, 5L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 15L, 16L, 2L, 14L, 2L, 4L, 5L, 6L, 7L, 9L, 10L
), .Label = c("", "ANT/SO", "ARC", "EPR/GAL", "GOM/CAR", "IND",
"MAR", "MED", "N-ATL", "NE-ATL", "NE-PAC", "NW-ATL", "NW-PAC",
"SE-ATL", "SE-PAC", "SW-ATL", "SW-PAC"), class = "factor"), Group3 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, …Run Code Online (Sandbox Code Playgroud) 从数据框架中,我想绘制五个类别的饼图,其百分比作为同一图表中的标签,从最高到最低顺序,顺时针方向.
我的代码是:
League<-c("A","B","A","C","D","E","A","E","D","A","D")
data<-data.frame(League) # I have more variables
p<-ggplot(data,aes(x="",fill=League))
p<-p+geom_bar(width=1)
p<-p+coord_polar(theta="y")
p<-p+geom_text(data,aes(y=cumsum(sort(table(data)))-0.5*sort(table(data)),label=paste(as.character(round(sort(table(data))/sum(table(data)),2)),rep("%",5),sep="")))
p
Run Code Online (Sandbox Code Playgroud)
我用
cumsum(sort(table(data)))-0.5*sort(table(data))
Run Code Online (Sandbox Code Playgroud)
将标签放在相应的部分和
label=paste(as.character(round(sort(table(data))/sum(table(data)),2)),rep("%",5),sep="")
Run Code Online (Sandbox Code Playgroud)
对于标签,这是百分比.
我得到以下输出:
Error: ggplot2 doesn't know how to deal with data of class uneval
Run Code Online (Sandbox Code Playgroud) 我有一个圆形空间,角度0和360是等价的.我想在这个空间中绘制矩形,使矩形可以穿过这个值.但是,我遇到了ggplot2的问题.
base <- ggplot() +
scale_x_continuous(breaks = seq(45, 360, 45), limits = c(0, 360)) +
scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
coord_polar(theta = "x", start = 1.5 * pi, direction = -1)
Run Code Online (Sandbox Code Playgroud)
1.尝试绘制超过xlim的值:
base + geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6),
color = "darkblue", fill = "steelblue")
#> Warning message:
#> Removed 1 rows containing missing values (geom_rect).
Run Code Online (Sandbox Code Playgroud)
xlim之外的所有值都被删除,因此这不起作用.
2.尝试使用重新调整的值进行绘图
base + geom_rect(aes(xmin = 340, xmax = 380 %% 360, …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) 如何使用两个不同大小的半圆(或其他形状,如三角形等)制作这样的图?
我已经研究了几个选项:另一个帖子建议使用一些unicode符号,这对我不起作用.如果我使用矢量图像,如何正确调整尺寸参数,使2个圆圈相互接触?
样本数据(我想使两个半圆的大小等于circle1size和circle2size):
df = data.frame(circle1size = c(1, 3, 2),
circle2size = c(3, 6, 5),
middlepointposition = c(1, 2, 3))
Run Code Online (Sandbox Code Playgroud)
最终有没有办法将半圆定位在不同的y值,编码第三维,就像这样?

任何建议都非常感谢.
我在这里检查了其他问题,但我看不出这个问题.我有标签问题.奇怪的是,除了一个标签之外,所有标签的代码都运行良好.当我检查数据集时(这非常简单),一切看起来都很好(一列有因子变量,另一列有数字).
这很奇怪,因为它适用于具有相同结构的其他一些数据.但是,我尝试/检查了一切,但无法解决这个问题.这是问题所在:
library(ggplot2)
library(ggrepel)
df = data.frame(
status = c("Oak", "maple", "walnut", "Pine"),
value = c( 47.54, 37.70, 11.48, 3.28))
ggplot(df, aes(x = "" , y = value, fill = fct_inorder(status))) +
geom_bar(width = 1, stat = "identity") +
coord_polar(theta = "y", start = 0 ) +
scale_fill_brewer(palette = "Set3", direction = -4) +
geom_label_repel(aes(label = paste0(value, "%")), size=4, show.legend = F, nudge_x = 1) +
guides(fill = guide_legend(title = "Status")) +
theme_void()
Run Code Online (Sandbox Code Playgroud)
如果我至少有一个建议来尝试或解释这种奇怪的行为,那就太好了.
显然,通过新的ggplot2更新,他们在不提供任何额外位置数据的情况下计算出位置问题,但不知何故,如果由于技术限制而无法使用它,这可能有助于解决此类问题.
我使用聚合(,, 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) 饼图:

数据链接:https://drive.google.com/file/d/0BwoPt0jyGdzORkM3cVA0WjJodVk/view?usp =sharing
Mydata<-read.csv(file="final_analysis_candy_analysis.csv",head=TRUE,sep=",")
dhfr.Arg <- table(Mydata$dhfr.Arg.59.163.137.)
dhfr.Ile <- table(Mydata$dhfr.Ile.51.214.65.)
dhfr.Asn108 <- table(Mydata$dhfr.Asn108.328.372.)
glu.540 <- table(Mydata$glu.540.538.326.200.)
gly.437 <- table(Mydata$gly.437.848.300.)
library(plotrix)
par(op)
op <-par(mfrow=c(2,3),mar=c(0,0,1,0))
pct <- round(dhfr.Arg/sum(dhfr.Arg)*100)
lbls <- paste(names(dhfr.Arg), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp<-pie3D(dhfr.Arg,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="dhfr Arg 59(163,137)")
pct <- round(dhfr.Ile/sum(dhfr.Ile)*100)
lbls <- paste(names(dhfr.Ile), pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
lp<-pie3D(dhfr.Ile,radius=0.8,labels=lbls,explode=0.1,
labelrad=1.4,main="dhfr Ile 51(214,65)")
pct <- round(dhfr.Asn108/sum(dhfr.Asn108)*100)
lbls <- paste(names(dhfr.Asn108), pct) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将标签与我的饼图与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)
我读了这些主题,但没有成功: