R 更改坐标图的半径

R 更改坐标图的半径,r,ggplot2,pie-chart,axis-labels,R,Ggplot2,Pie Chart,Axis Labels,我希望缩小ggplot2中构建的饼图相对于绘图其余部分的显示半径(因为默认设置会一直切断我的类别标签) 以下是一些虚拟数据和代码,可以向您展示我的体验: library(ggplot2) library(scales) library(grid) Region <- c("North America", "Central America", "South America", "Carribbean", "Western Africa", "Northern Afr

我希望缩小ggplot2中构建的饼图相对于绘图其余部分的显示半径(因为默认设置会一直切断我的类别标签)

以下是一些虚拟数据和代码,可以向您展示我的体验:

library(ggplot2)
library(scales)
library(grid)

Region <- c("North America", "Central America", "South America", "Carribbean",
            "Western Africa", "Northern Africa", "Southern Afica", "Eastern Africa")
Conti <- c(rep("Americas",4), rep("Africa",4))
Freq <- c(runif(8, 1, 100))
Pct <- c(Freq/sum(Freq))
Pos <- c(cumsum(360*Pct)-(360*Pct/2))
Pos <- c(ifelse(Pos<=180,Pos,Pos-180))
df <- data.frame(Region, Conti, Freq, Pct, Pos)

pl <- ggplot(df, aes(x="", y=Freq, fill=Conti)) +
  geom_bar(stat="identity", color="black", width=1) +
  coord_polar(theta='y') +
  guides(fill=guide_legend(override.aes=list(colour=NA))) +
  theme(axis.line = element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text.y=element_blank(),
        axis.text.x=element_text(color='black', size=18, angle=90-df$Pos),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.margin = unit(0, "lines"),
        plot.background = element_rect(fill = "white"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        legend.position = "none") +
  scale_y_continuous(
    breaks=cumsum(df$Freq) - df$Freq/2,
    labels=paste0(df$Region," ",percent(df$Pct)))

print(pl)
库(ggplot2)
图书馆(比例尺)
图书馆(网格)

地区我的建议是使用换行符

pl <- pl + scale_y_continuous(
  breaks=cumsum(df$Freq) - df$Freq/2,
  labels=paste0(sapply(strsplit(as.character(df$Region), " "), paste, collapse='\n'),
                "\n(", percent(df$Pct), ")"))
ggsave('pie.png', plot=pl, height=15, width=15)

pl我的建议是使用换行符

pl <- pl + scale_y_continuous(
  breaks=cumsum(df$Freq) - df$Freq/2,
  labels=paste0(sapply(strsplit(as.character(df$Region), " "), paste, collapse='\n'),
                "\n(", percent(df$Pct), ")"))
ggsave('pie.png', plot=pl, height=15, width=15)

pl几个月后,我发现答案在于为X使用一个数字伪值(而不是空值“”),然后向伪X轴添加大于X的限制。代码如下

然后,问题变成调整中的轴标签以与新半径对齐,因为
hjust=
vjust=
似乎不适用于
coord\u polar()

为此,我将标签添加为
geom_text(
),并删除了自动标签。这正是我需要的

关键的变化在顶部和底部

pl <- ggplot(df, aes(x=0.8, y=Freq, fill=Conti)) +
  geom_bar(stat="identity", color="black", width=1) +
  coord_polar(theta='y') +
  geom_text(aes(x=1.65, y=cumsum(df$Freq) - df$Freq/2,
            label=paste0(df$Region," ",percent(df$Pct)),
            angle=90-df$Pos)) +
  guides(fill=guide_legend(override.aes=list(colour=NA))) +
  theme(axis.line = element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.background = element_rect(fill = "white"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        legend.position = "none") +
  scale_x_discrete(limits=c(0, 1))

print(pl)

pl几个月后,我发现答案在于为X使用一个数字伪值(而不是空值“”),然后向伪X轴添加大于X的限制。代码如下

然后,问题变成调整中的轴标签以与新半径对齐,因为
hjust=
vjust=
似乎不适用于
coord\u polar()

为此,我将标签添加为
geom_text(
),并删除了自动标签。这正是我需要的

关键的变化在顶部和底部

pl <- ggplot(df, aes(x=0.8, y=Freq, fill=Conti)) +
  geom_bar(stat="identity", color="black", width=1) +
  coord_polar(theta='y') +
  geom_text(aes(x=1.65, y=cumsum(df$Freq) - df$Freq/2,
            label=paste0(df$Region," ",percent(df$Pct)),
            angle=90-df$Pos)) +
  guides(fill=guide_legend(override.aes=list(colour=NA))) +
  theme(axis.line = element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        panel.background = element_blank(),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        plot.background = element_rect(fill = "white"),
        plot.margin = unit(c(0, 0, 0, 0), "cm"),
        legend.position = "none") +
  scale_x_discrete(limits=c(0, 1))

print(pl)

pl我没有想到在标签中使用换行符(在区域和百分比之间使用换行符就可以了)。如果可能的话,我仍然希望改善文本与饼图之间的视觉平衡。我没有想到在标签中使用换行符(区域和百分比之间的换行符就可以了)。如果可能的话,我仍然希望改善文本与饼图的视觉平衡。