r - how to change aspect ratio of the PANEL (grey area) in ggplot2 -
hi i'm plotting pie chart , putting names on there. , text cropped outside panel (grey area).
is there way change aspect ratio of panel?
i tried theme(aspect.ratio=1.5)
, changes pie chart ellipse. want pie chart not change, , want panel wider. , tried adjust size of picture, doesn't change panel (grey area).
any help's appreciated. thanks!
library(ggplot2) source('piechart.r') df = data.frame('name' = c('long long name one', 'long long name two', 'long long name three', 'long name long four'), 'count' = c(10, 20, 30, 40)) png(width=1000, height=600, file='/users/yuji/onedrive/data/townstate/pie.png') piechart(df, 'name', 'count') dev.off()
piechart.r
piechart = function (data1, name1, value1){ position = cumsum(data1[[value1]])-0.5*data1[[value1]] ggplot(data = data1, aes_string(x=1, y=value1, fill=name1)) + geom_bar(stat="identity") + coord_polar(theta ='y') + theme(axis.ticks=element_blank(), axis.title=element_blank(), axis.text.y=element_blank(), axis.text.x=element_text(colour='black'), legend.position='none', # panel.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank()#, # aspect.ratio=1.5 ) + scale_y_continuous(breaks=position, labels=data1[[name1]]) }
i'm not sure it's possible: aspect ratio needs unity pie circular. workaround, turn off clipping , plot on top of grey background.
b = ggplotgrob(p + theme(plot.background=element_rect(fill=na,colour=na))) b$layout$clip[b$layout$name=="panel"] = "off" grid.draw(grobtree(rectgrob(gp=gpar(fill="grey92")), b))
alternatively, manually edit widths/heights of gtable. first step replace null units of panel "snpc", remain square, change margins "null" units expand as possible.
library(grid) library(gtable) p2 <- p + theme(plot.background=element_rect(fill="grey92")) g <- ggplotgrob(p2) g$layout$clip[g$layout$name=="panel"] <- "off" g$respect <- false grid.newpage() g$widths[[4]] <- unit(1,"snpc") g$heights[[3]] <- unit(1,"snpc") g$widths[c(1,5)] <- rep(list(unit(1,"null")), 2) g$heights[c(1,6)] <- rep(list(unit(1,"null")), 2) grid.draw(g)
Comments
Post a Comment