【问题标题】:Change plot area of ggplot2 pie chart更改ggplot2饼图的绘图区域
【发布时间】:2015-10-05 12:35:25
【问题描述】:

谁能告诉我如何更改绘图区域以使所有标签都适合并且不会被剪切,或者缩小饼图的大小。

这是我使用的ggpie函数

ggpie <- function (dat, by, totals,perc) {
ggplot(dat, aes_string(x=factor(1), y=totals, fill=by, width =0.5)) +
geom_bar(stat='identity', color='black') +
guides(fill=F) +
scale_fill_manual("Genes", values = gc1)+
coord_polar(theta='y',start=1) +  
theme(axis.ticks=element_blank(),
      axis.text.y=element_blank(),
      panel.grid=element_blank(),
      axis.text.x=element_text(colour='black',size=12,face='bold'),
      axis.title=element_blank(),
      plot.margin=unit(c(1,10,1,1),"cm"))+
scale_y_continuous(breaks=cumsum(dat[[totals]]) - dat[[totals]] / 2, labels=paste("example", dat[[perc]],"%"))}

这是我调用函数的地方

GP <- ggpie(high, by='Genes', totals='Count',perc='Percent')+
theme(plot.title = element_text(face='bold', size=16))+
theme(axis.ticks.margin=unit(0,"lines"),plot.margin=rep(unit(0, "lines"),4)) +
theme(legend.title=element_text(size=16))+
theme(legend.text=element_text(face='bold'))
print(GP)

如你所见,我已经尝试过plot.margin,也尝试过par(mar=c(), oma=c())

这似乎是一些缩放问题,例如,当我在 r studio 中使用缩放功能查看图形时,这很好。但是当你保存它时它不是......

我希望有人可以帮助我增加面积或缩放馅饼。

示例数据:

    Genes   Count   Frequency   Percent
1   example 43,5427786  0,228215768 22,8215768
2   example 20,583859   0,107883817 10,7883817
3   example 16,6254246  0,087136929 8,7136929
4   example 15,8337377  0,082987552 8,2987552
5   example 13,458677   0,070539419 7,0539419
6   example 7,9168688   0,041493776 4,1493776
7   example 6,3334951   0,033195021 3,3195021
8   example 6,3334951   0,033195021 3,3195021
9   example 6,3334951   0,033195021 3,3195021
10  example 5,5418082   0,029045643 2,9045643
11  example 4,7501213   0,024896266 2,4896266
12  example 3,9584344   0,020746888 2,0746888
13  example 3,9584344   0,020746888 2,0746888
14  example 3,9584344   0,020746888 2,0746888
15  example 3,9584344   0,020746888 2,0746888
16  example 3,1667475   0,01659751  1,659751
17  example 2,3750607   0,012448133 1,2448133
18  example 2,3750607   0,012448133 1,2448133
19  example 2,3750607   0,012448133 1,2448133
20  example 2,3750607   0,012448133 1,2448133
21  example 1,5833738   0,008298755 0,8298755
22  example 1,5833738   0,008298755 0,8298755
23  example 1,5833738   0,008298755 0,8298755
24  example 1,5833738   0,008298755 0,8298755
25  example 1,5833738   0,008298755 0,8298755
26  example 0,7916869   0,004149378 0,4149378
27  example 0,7916869   0,004149378 0,4149378
28  example 0,7916869   0,004149378 0,4149378
29  example 0,7916869   0,004149378 0,4149378
30  example 0,7916869   0,004149378 0,4149378
31  example 0,7916869   0,004149378 0,4149378
32  example 0,7916869   0,004149378 0,4149378
33  example 0,7916869   0,004149378 0,4149378
34  example 0,7916869   0,004149378 0,4149378

【问题讨论】:

  • 请附上您的数据样本(我猜是dat)。
  • 饼图是出了名的难以解释。条形图呢?
  • 我知道人们不喜欢它们,但它们对于快速掌握混合物的相对成分非常有帮助
  • @Robin 还有其他方法可以做到这一点,而人类并不擅长估计角度。

标签: r plot ggplot2 pie-chart


【解决方案1】:

通过增加绘图区域的绝对大小,您的标签将变得相对较小(因为它们具有固定的点大小)。如果您在 RStudio 中缩放视图,就会发生这种情况。 当您使用ggsavepng (helpful SO question here) 保存您的绘图并指定更大的绝对大小时,您会注意到您的绘图被清除了。

(如果你使用dput(see here)导出部分数据,我给你举个例子。)

Here are some more tips for producing proper R graphics,包括关于 dpi 和尺寸的一些详细信息。

【讨论】:

  • Tnxs 提示!!,问题是我不想最终得到一个巨大的馅饼。如果我以较大的绝对大小保存它,然后将图片重新缩放到我想要的大小,则文本将变得不可读。我可以改变馅饼的比例大小吗?
  • 不确定,但是这个问题的一些“相关问题”(在本页右侧)可能会给你一些想法(例如this one)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2018-04-24
  • 2019-01-15
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多