gpt4 book ai didi

r - 如何使用 ggplot2 R 增加整个图的宽度

转载 作者:行者123 更新时间:2023-12-04 12:12:07 24 4
gpt4 key购买 nike

我一直在使用 ggplot2 绘制一个图形,该图形看起来像一个热图,最右边和最上面的行和列分别显示其元素的总和。然而,这是一个巨大的数据集,所有的“总和”数字都被挤进了里面,而且不清楚。我想扩大情节,使标签看起来可读。到目前为止,这是我的代码。

mat <- read.csv("trial.csv",sep = ",", header=T, row.names=1)
vec <- as.vector(as.matrix(mat))
varieties = names(mat)
matVar = matrix(vec, ncol = length(varieties), nrow = length(attr(mat, "row.names")))
library(ggplot2)
require(reshape2)
library(grid)
require(grid)
mat$id <- rownames(mat)
gg <- melt(mat)
#mat$id <- reorder(mat$id)
#gg$id<-factor(gg$id)
gg$id <- as.character(gg$id)
gg$id <- factor(gg$id, levels=rev(unique(gg$id)))
ggplot(gg, aes(y=variable,x=id))+
geom_tile(aes(fill=value))+
scale_fill_gradient(low="red",high="white")+
geom_text(aes(label=value), data=cbind(aggregate(value~variable, gg, sum), id="total")) +
geom_text(aes(label=value),data=cbind(aggregate(value~id, gg, sum), variable="total")) +
theme(axis.text.x = element_blank())+
scale_y_discrete(limits=c(levels(gg$variable), "total"))+
scale_x_discrete(limits=c(levels(gg$id), "total"))+
coord_flip()+labs(y = "Patients", x= "Genes")

这是示例数据,但我的实际数据集非常庞大。
h1, h2, h3, h4, h5, h6, h7, h8, h9
a, 1, 1, 1, 0, 1, 1, 0, 1
b, 0, 1, 1, 1, 0, 0, 0, 0
c, 1, 0, 0, 1, 1, 1, 1, 1
d, 1, 0, 1, 0, 0, 0, 1, 0
e, 1, 0, 0, 0, 0, 1, 0, 0
f, 1, 1, 0, 0, 0, 0, 0, 0
g, 0, 0, 0, 0, 0, 0, 0, 0
h, 0, 0, 0, 0, 0, 1, 1, 0

这是输出
enter image description here

最佳答案

尝试指定图形设备的尺寸,例如

pdf(file="plot.pdf",width=11,height=8)

PDF 使用以英寸为单位的尺寸,而 PNG 以像素为单位,因此您可能还需要指定分辨率,例如
png(file="animals45.png",width=400,height=350,res=45)

有关更多信息,请参阅:
  • http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/pdf.html
  • http://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html
  • 关于r - 如何使用 ggplot2 R 增加整个图的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240678/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com