标签: cowplot

加入树状图和热图

我有heatmap(来自一组样本的基因表达):

set.seed(10)
mat <- matrix(rnorm(24*10,mean=1,sd=2),nrow=24,ncol=10,dimnames=list(paste("g",1:24,sep=""),paste("sample",1:10,sep="")))
dend <- as.dendrogram(hclust(dist(mat)))
row.ord <- order.dendrogram(dend)
mat <- matrix(mat[row.ord,],nrow=24,ncol=10,dimnames=list(rownames(mat)[row.ord],colnames(mat)))
mat.df <- reshape2::melt(mat,value.name="expr",varnames=c("gene","sample"))

require(ggplot2)
map1.plot <- ggplot(mat.df,aes(x=sample,y=gene))+geom_tile(aes(fill=expr))+scale_fill_gradient2("expr",high="darkred",low="darkblue")+scale_y_discrete(position="right")+
  theme_bw()+theme(plot.margin=unit(c(1,1,1,-1),"cm"),legend.key=element_blank(),legend.position="right",axis.text.y=element_blank(),axis.ticks.y=element_blank(),panel.border=element_blank(),strip.background=element_blank(),axis.text.x=element_text(angle=45,hjust=1,vjust=1),legend.text=element_text(size=5),legend.title=element_text(size=8),legend.key.size=unit(0.4,"cm"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

(由于plot.margin我正在使用的论据,左侧被切断,但我需要这个,如下所示).

然后,我prunedendrogram根据深度截止值来获得较少的集群(即,只有深深的分裂),并做一些编辑所产生dendrogram有它绘制他们的方式,我希望它:

depth.cutoff <- 11
dend <- cut(dend,h=depth.cutoff)$upper
require(dendextend)
gg.dend <- as.ggdend(dend)
leaf.heights <- dplyr::filter(gg.dend$nodes,!is.na(leaf))$height
leaf.seqments.idx <- which(gg.dend$segments$yend %in% leaf.heights)
gg.dend$segments$yend[leaf.seqments.idx] <- max(gg.dend$segments$yend[leaf.seqments.idx])
gg.dend$segments$col[leaf.seqments.idx] <- "black"
gg.dend$labels$label <- 1:nrow(gg.dend$labels)
gg.dend$labels$y <- max(gg.dend$segments$yend[leaf.seqments.idx])
gg.dend$labels$x <- gg.dend$segments$x[leaf.seqments.idx]
gg.dend$labels$col <- "black"
dend1.plot <- ggplot(gg.dend,labels=F)+scale_y_reverse()+coord_flip()+theme(plot.margin=unit(c(1,-3,1,1),"cm"))+annotate("text",size=5,hjust=0,x=gg.dend$label$x,y=gg.dend$label$y,label=gg.dend$label$label,colour=gg.dend$label$col)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 我用cowplot's将它们绘制在一起plot_grid:

require(cowplot)
plot_grid(dend1.plot,map1.plot,align='h',rel_widths=c(0.5,1)) …
Run Code Online (Sandbox Code Playgroud)

r dendrogram heatmap ggplot2 cowplot

24
推荐指数
1
解决办法
2300
查看次数

如何在牛皮画中将普通的ggplot与刻面的ggplot对齐?

我正试图通过使用cowplot包来以出版物的方式安排绘图.
我只是希望面板的尺寸和尺寸相同.

可重复的示例

library(ggplot2)
library(cowplot)

gg1 <- ggplot(mtcars)+
        geom_point(aes(x=mpg,y=hp))+
        theme_bw()+
        theme(aspect.ratio=1)

gg2 <- ggplot(mtcars)+
        geom_point(aes(x=mpg,y=hp,fill=cyl))+
        facet_wrap(~cyl,ncol=2)+
        theme_bw()+
        theme(aspect.ratio=1,
              legend.position='none')

output <- plot_grid(gg1,gg2, labels = c('A','B'),label_size = 20)
print(output)
Run Code Online (Sandbox Code Playgroud)

代码生成此图. 在此输入图像描述

如您所见,水平轴既不匹配,也不匹配面板的上边缘.

这个论点aligncowplot不方位图工作.

有任何想法吗?

r ggplot2 cowplot

18
推荐指数
2
解决办法
2487
查看次数

Cowplot让ggplot2主题消失/如何查看当前的ggplot2主题,并恢复默认?

我最近安装了这个cowplot包.然而,在这样做之后,我注意到我的ggplots缺少他们的背景和网格线theme_grey()!

在此输入图像描述

创建上述每个图的代码是:

result_df %>%
    ggplot(aes_string(x = 'p', y = 'r')) +
    # theme_grey() + # uncomment this line to produce plot on right
    geom_point(aes(group = c), size = 0.5) +
    geom_line(aes(group = c), size = 0.2, linetype = 'dotted') +
    theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5)) +
    facet_grid(b ~ e, scales = "free_y") +
    scale_x_continuous(breaks = seq(0, 10, 2))
Run Code Online (Sandbox Code Playgroud)

没有明确调用+ theme_grey(),我得到左边的情节.

这里发生了什么?我以为那theme_grey()是默认的.如何查看我的默认主题是什么?

这是我的一个片段sessionInfo():

R version 3.3.2 (2016-10-31) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 cowplot

15
推荐指数
1
解决办法
3723
查看次数

禁用ggplots的cowplot默认值

ggplot2附加软件包,cowplot具有用来绘制多条曲线称为一个不错的功能plot_grid().这是plot_grid()在行动:

library(ggplot2); library(cowplot)

plot_a <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
plot_b <- ggplot(mtcars, aes(mpg, disp)) + geom_point()

plot_grid(plot_a, plot_b, labels = c("A", "B"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但请注意加载如何cowplot更改默认的绘图样式.如何加载cowplot包以便我可以使用该plot_grid()功能,但也禁用cowplot强制执行的默认打印样式?

plot visualization r ggplot2 cowplot

12
推荐指数
1
解决办法
2441
查看次数

在R中使用cowplot制作ggplot图表占据连续两行

这是我的代码:

library(ggplot2)
library(cowplot)


df <- data.frame(
  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4
)

p1 <- ggplot(df, aes(x, y1)) + geom_point()
p2 <- ggplot(df, aes(x, y2)) + geom_point()
p3 <- ggplot(df, aes(x, y3)) + geom_point()
p4 <- ggplot(df, aes(x, y4)) + geom_point()
p5 <- ggplot(df, aes(x, y3)) + geom_point()
# simple grid
plot_grid(p1, p2, 
          p3, p4,
          p5, p4)
Run Code Online (Sandbox Code Playgroud)

但我不想重复p4我想“拉伸”p4以占据col2以及第2行和第3行。

有什么帮助吗?

r ggplot2 cowplot

10
推荐指数
2
解决办法
693
查看次数

R中的牛皮网格的一个共享传奇

我刚刚用包构建了一个网格cowplot(用来标记AD的图).这些地块是用包装制成的ggplot2:

pfour<-ggplot(four, aes(x=Concentration, y=Percentage, fill=Phenotype)) + 
 geom_bar(stat='identity',color='black') +
 scale_fill_grey(start = .4, end = .9) + 
 theme_bw()+ylab("Distribution") + 
 xlab("Contentration [mg/ml]") + 
 ggtitle("96 hpf") +
 theme(legend.title = element_text(colour="black", size=10, face="bold")) +
 theme(legend.background = element_rect(fill="white",
                                        size=0.5, linetype="solid", 
                                        colour ="black")) +
 scale_x_discrete(limits=c('uninjected','control','0.002', '0.02', '0.2'),
                  labels=c('uninjected\n(n=251)',
                           'control\n(n=248)', 
                           '0.002\n(n=205)', 
                           '0.02\n(n=222)', 
                           '0.2\n(n=203)'))
Run Code Online (Sandbox Code Playgroud)

数据看起来像那样(4个不同的表,百分比略有不同但原理相同):

Concentration,Percentage,Phenotype
uninjected,0.996015936,0
uninjected,0,1
uninjected,0.003984064,2
uninjected,0,3
uninjected,0,4
control,0.995967742,0
control,0.004032258,1
control,0,2
control,0,3
control,0,4
0.002,0.985365854,0
0.002,0.004878049,1
0.002,0.004878049,2
0.002,0,3
0.002,0.004878049,4
0.02,0.981981982,0
0.02,0.004504505,1
0.02,0.004504505,2
0.02,0.004504505,3
0.02,0.004504505,4
0.2,0.985221675,0
0.2,0.004926108,1
0.2,0,2
Run Code Online (Sandbox Code Playgroud)

它看起来像那样:

情节

代码是:

plot_grid(ponezoom, ptwozoom,pthreezoom,pfourzoom, align='h', labels=c('A', …
Run Code Online (Sandbox Code Playgroud)

r legend r-grid cowplot

8
推荐指数
2
解决办法
6688
查看次数

使用ggplot2合并并完美对齐直方图和Boxplot

从昨天开始,我正在阅读答案和网站,以便在一个图表中合并和对齐,histogramboxplot使用ggplot2包生成.

这个问题与其他问题不同,因为boxplot chart需要减少在左边缘heightaligned左边缘histogram.

考虑以下数据集:

my_df <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, …
Run Code Online (Sandbox Code Playgroud)

r histogram ggplot2 boxplot cowplot

8
推荐指数
1
解决办法
1961
查看次数

与cowplot和plot_grid共享面板边框

我正在尝试在两个与plot_grid牛皮画包装对齐的地块周围绘制边框.请参阅以下示例(从"更改轴位置"晕影修改):

require(gtable)
require(cowplot)

# top plot
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = 'blue') +
  background_grid(minor = 'none')
g1 <- switch_axis_position(p1, 'xy') # switch both axes
g1 <- gtable_squash_rows(g1, length(g1$height)) # set bottom row to 0 height

# bottom plot
p2 <- ggplot(mtcars, aes(mpg, qsec)) + geom_line(colour = 'green') + ylim(14, 25) +
  background_grid(minor = 'none')
g2 <- ggplotGrob(p2)
g2 <- gtable_add_cols(g2, g1$widths[5:6], 4) # add the two additional columns that g1 has
g2 <- …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 cowplot

7
推荐指数
1
解决办法
724
查看次数

是否可以避免轴标签被ggrepel重叠?

我正在用ggplot2绘制热图。y轴上的几个刻度需要标记。但是,其中一些过于紧密和重叠。我知道ggrepel可以分隔文本标签,但是目前我还没有解决我的问题。

我的代码如下。任何建议都欢迎。谢谢。

码:

df <- data.frame()

for (i in 1:50){
  tmp_df <- data.frame(cell=paste0("cell", i), 
                       gene=paste0("gene", 1:100), exp = rnorm(100), ident = i %% 5)
  df<-rbind(df, tmp_df)
}

labelRow=rep("", 100)
for (i in c(2, 5, 7, 11, 19, 23)){
  labelRow[i] <- paste0("gene", i)
}

library(ggplot2)
heatmap <- ggplot(data = df, mapping = aes(x = cell, y = gene, fill = exp)) +
  geom_tile() + 
  scale_fill_gradient2(name = "Expression") + 
  scale_y_discrete(position = "right", labels = labelRow) +
  facet_grid(facets = ~ident,
             drop = TRUE, …
Run Code Online (Sandbox Code Playgroud)

r facet ggplot2 cowplot ggrepel

7
推荐指数
1
解决办法
991
查看次数

如何对齐并避免单独创建然后添加到图中的多个图例重叠?

我正在尝试创建一个结合了 2 个独立图例和多个绘图网格的绘图。我遇到的问题是我发现很难对齐图例以使它们可见且不重叠。希望下面的例子能够解释我的意思。

首先,我将创建 2 个图。在这两个图中,我只对图例感兴趣,并且我放弃了实际的图(所以请忽略这两个图中的实际图)。为了获得图例,我正在使用该cowplot包。

library(ggplot2)
library(cowplot)
# -------------------------------------------------------------------------
# plot 1 ------------------------------------------------------------------

# create fake data
dfLegend_1 <- data.frame(x = LETTERS[1:10], y = c(1:10))
# set colours
pointColours <- c(A = "#F5736A", B = "#D58D00", C = "#A0A300",
                  D = "#36B300", E = "#00BC7B", F = "#00BCC2",
                  G = "#00ADF4", H = "#928DFF", I = "#E568F0",
                  J = "#808080")

# plot
ggLegend_1 <- ggplot(dfLegend_1, aes(x=x, y=y))+
  geom_point(aes(fill = pointColours), shape = 22, size = 10) + …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 cowplot

7
推荐指数
1
解决办法
623
查看次数

标签 统计

cowplot ×10

r ×10

ggplot2 ×9

boxplot ×1

dendrogram ×1

facet ×1

ggrepel ×1

heatmap ×1

histogram ×1

legend ×1

plot ×1

r-grid ×1

visualization ×1