改变coord_polar ggplot的半径 [英] Changing the radius of a coord_polar ggplot

查看:143
本文介绍了改变coord_polar ggplot的半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找减少ggplot2内建的饼图相对于其余图的显示半径(因为默认值会切断我的分类标签)。



  library(ggplot2)$ b $这是一些虚拟数据和代码,它们会告诉你我正在经历什么: b图书馆(比例)
图书馆(网格)

区域<-c(北美洲,中美洲,南美洲,加勒比海,
非洲,北非,南非,东非)
Conti <-c(rep(美洲,4),rep(非洲,4))$ b (频率/和(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(ov (),
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)

如果我减小标签的大小,它们会相对于馅饼变得难以辨认,如果我增加它们,它们会被切断。无论我如何调整aes(),限制,panel.margin等以获得平衡,ggplot2都会自动重新调整饼图的大小以占据相同的半径。



理想情况下,我想将馅饼缩小一半,以便为标签留出更多空间。



我明白这不是最漂亮的情节,但是,我正在更新一个旧图,所以需要保持格式进行比较。任何建议,将不胜感激。

解决方案

几个月后,我发现答案在于使用X的数值虚拟值(而不是空值),然后将大于x的限制添加到虚拟x轴。代码如下。



然后,问题就变成调整轴标签以与新半径对齐,如 hjust = vjust = 似乎不适用于 coord_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(color = 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(),
面板。 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)


I'm looking to reduce the display radius of a pie chart built in ggplot2 relative to the rest of the plot (as the defaults keep cutting off my category labels).

Here's some dummy data and code that should show you what I'm experiencing:

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)

If I reduce the size of the labels they become illegible relative to the pie and if I increase them they get cut-off. No matter how I try to adjust the aes(), limits, panel.margin etc. to get the balance right ggplot2 keeps automatically re-sizing the pie to occupy the same radii.

Ideally, I'd like to shrink the pie by half to allow more room for labels.

I appreciate that this isn't the prettiest plot, however, I'm updating an old figure so need to maintain the format for comparison. Any suggestions would be appreciated.

解决方案

Some months later I've found that the answer lies in using a numeric dummy value for X (rather than a null value "") and then adding limits that are larger than x to the dummy x-axis. Code as below.

The issue then becomes adjusting the axis labels in to align with the new radius as hjust= and vjust= don't seem to work with coord_polar().

To do this I've added the labels as geom_text() and removed the automatic labels. This now does what I need.

The key changes are in the top and bottom lines.

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)

这篇关于改变coord_polar ggplot的半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆