我想知道如何修改下面的代码来控制绘图的大小(例如:我如何使y元素彼此更接近以使绘图更小)
Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data <- data.frame(Year, Category, Frequency)
library(dplyr)
Data <- group_by(Data,Year) %>%
mutate(pos = cumsum(Frequency) - (0.5 * Frequency))
library(ggplot2)
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
geom_bar(aes(fill = Category), stat="identity", show.legend = FALSE) +
geom_text(aes(label = Frequency, y = pos), size = 3, nudge_y = -25) +
geom_text(aes(label = Category, y = pos), size = 3, nudge_y = 25)
答案 0 :(得分:2)
尝试使用cowplot包来绘制绘图窗口中的绘图
library(cowplot)
ggdraw()+draw_plot(p,x=0,y=0,width=.5,height = .5)
答案 1 :(得分:1)
我只是想在特定尺寸上保存图表,然后您可以将图表导出为pdf,如下所示:
pdf("C:/mypdf.pdf", width=5, height=5)
p
dev.off()
如果您打算删除空白区域并使用过的绘图区域更小,您可以为条形指定特定宽度以移除条形之间的空间,将比例扩展到绘图边缘,删除绘图边距(库)需要网格),并在图中移动图例:
library(grid)
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
geom_bar(aes(fill = Category,width=.95), stat="identity", show.legend = FALSE) +
geom_text(aes(label = Frequency, y = pos), size = 3, nudge_y = -25) +
geom_text(aes(label = Category, y = pos), size = 3, nudge_y = 25) +
theme( plot.margin = unit( c(0,0,0,0) , "in" ) ) +
scale_x_discrete( expand = c(0, 0)) + scale_y_continuous(expand = c(0,0)) +
theme(legend.position=c(1,1),legend.justification=c(1, 1),legend.box.just="left")