R 自动调整使用ggplot2绘制的绘图的图例大小,以便整个图例位于图层边界内

R 自动调整使用ggplot2绘制的绘图的图例大小,以便整个图例位于图层边界内,r,ggplot2,themes,legend,margins,R,Ggplot2,Themes,Legend,Margins,我有一些数据 我将数据读入一个数据框,然后用下面的代码绘制数据 # Reading data from a .csv file into a data frame df <- read.table("newcsv_file.csv",header=T,sep="\t" ) # Now melting the data frame prior to plotting df_mlt <- melt(df, id=names(df)[1], measure=names(

我有一些数据

我将数据读入一个数据框,然后用下面的代码绘制数据

# Reading data from a .csv file into a data frame
     df <- read.table("newcsv_file.csv",header=T,sep="\t" )
# Now melting the data frame prior to plotting     
df_mlt <- melt(df, id=names(df)[1], measure=names(df)[c(2, 6, 11,16,21,26,31,36,41,46,51,106,111,116,121,126,131,136,141,146,151)], variable = "cols")

# plotting the data
plt_fit <- ggplot(df_mlt, aes(x=x,y= value, color=cols)) +
    geom_point(size=2) +
    geom_smooth(method = "lm", se = FALSE) +
    scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
    annotation_logticks(sides = "rl") +
    theme_bw() + 
    theme(legend.text=element_text(size=12), legend.title=element_text(size=12))+
    theme(axis.text=element_text(size=14)) +
    theme(axis.title=element_text(size=14,face="bold")) +
    labs(x = "x", y = "y") +
    scale_color_discrete(name = "values", labels = c("0","-0.1","-0.2","-0.3","-0.4","-0.5","-0.6","-0.7","-0.8","-0.9","-1","+0.1","+0.2","+0.3","+0.4","+0.5","+0.6","+0.7","+0.8","+0.9","+1")) +
    guides(colour = guide_legend(override.aes = list(size=3),nrow=2,title.position = 'top',title.hjust=0.5,legend.direction = "horizontal")) +
    theme(legend.position = 'bottom', legend.margin=unit(1,"cm"),legend.background = element_rect(fill ='gray94')) +
    theme(plot.margin=unit(c(0,2,0,0),"mm"))
ggsave(file="new_png_file.png",width=22,height=21,units=c("cm"), dpi=600)
#将.csv文件中的数据读取到数据帧中

df代码很好。问题在于打印窗口的大小。试着把它弄宽一点,你就会看到整个传说

而且


将创建一个显示完整图例的pdf。

使用以下代码更改绘图的宽度和高度后

# Reading data from a .csv file into a data frame
     df <- read.table("newcsv_file.csv",header=T,sep="\t" )
# Now melting the data frame prior to plotting     
df_mlt <- melt(df, id=names(df)[1], measure=names(df)[c(2, 6, 11,16,21,26,31,36,41,46,51,106,111,116,121,126,131,136,141,146,151)], variable = "cols")

# plotting the data
plt_fit <- ggplot(df_mlt, aes(x=x,y= value, color=cols)) +
    geom_point(size=2) +
    geom_smooth(method = "lm", se = FALSE) +
    scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
    annotation_logticks(sides = "rl") +
    theme_bw() + 
    theme(legend.text=element_text(size=12), legend.title=element_text(size=12))+
    theme(axis.text=element_text(size=14)) +
    theme(axis.title=element_text(size=14,face="bold")) +
    labs(x = "x", y = "y") +
    scale_color_discrete(name = "values", labels = c("0","-0.1","-0.2","-0.3","-0.4","-0.5","-0.6","-0.7","-0.8","-0.9","-1","+0.1","+0.2","+0.3","+0.4","+0.5","+0.6","+0.7","+0.8","+0.9","+1")) +
    guides(colour = guide_legend(override.aes = list(size=3),nrow=2,title.position = 'top',title.hjust=0.5,legend.direction = "horizontal")) +
    theme(legend.position = 'bottom', legend.margin=unit(1,"cm"),legend.background = element_rect(fill ='gray94')) +
    theme(plot.margin=unit(c(0,2,0,0),"mm"))
ggsave(file="new_png_file.png",width=22,height=21,units=c("cm"), dpi=600)
产生这样一个情节


@jihoward谢谢你的建议。地块宽度和高度必须增加两倍于我保存的地块。