小编nou*_*use的帖子

ggplot2:没有填充的geom_polygon

我希望,你不需要这个问题的数据,因为我相信我只是犯了一个愚蠢的语法错误.以下代码:

ggplot()+
  geom_point(data=sites, aes(x=NMDS1, y=NMDS2, shape=group), colour="grey") +  
  geom_point(data=species, aes(x=NMDS1, y=NMDS2, color=phyla), size=3, shape=20) + scale_colour_manual(values=Pal1) +
  geom_segment(data = BiPlotscores, aes(x = 0, xend = NMDS1, y= 0, yend = NMDS2),
               arrow = arrow(length = unit(0.25, "cm")), colour = "black") +
  geom_text(data = BiPlotscores, aes(x = 1.1*NMDS1, y = 1.1*NMDS2, label = Parameters), size = 3) + coord_fixed()+
  theme(panel.background = element_blank()) +
  geom_polygon(data = hulls, aes(x=NMDS1, y=NMDS2, colour=phyla, alpha = 0.2))
Run Code Online (Sandbox Code Playgroud)

导致以下结果:

在此输入图像描述

(这不是最终产品:)).我希望多边形没有填充,或者只是整齐地填充.我不希望它们是灰色的,当然.填充不会做任何事情,显然摆弄alpha也不会改变任何事情.

任何想法都是超级受欢迎的.非常感谢你!

"Hulls"来自以下代码(在这里找到):

#find hulls
library(plyr)
find_hull <- …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

14
推荐指数
2
解决办法
3万
查看次数

在lapply中保存情节

我有一个数据帧列表:

str(subsets.d)
List of 22
 $ 1       :'data.frame':   358 obs. of  118 variables:
  ..$ Ac_2017_1 : num [1:358] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ Ac_9808_1 : num [1:358] 0 0 0 0 0 ...
  ..$ dates     : Ord.factor w/ 6 levels "April"<"May"<..: 1 1 1 1 1 1 1 
 $ 19      :'data.frame':   358 obs. of  2 variables:
  ..$ Ac_8598_19: num [1:358] 0.000257 0.000288 0.000171 0 0.000562 ...
  ..$ dates     : Ord.factor w/ 6 levels …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

在ax,y-scatter plot中使用直接标记

我在找到在ax,y-scatterplot中使用直接标记的最佳方法时遇到了问题.

library(ggplot2)
library(directlabels)
p1 <- ggplot()+
        geom_point(data=sites, aes(X, Y, col=Treatment), alpha=1,show_guide=FALSE) +
        geom_polygon(data = hulls, aes(X, Y, colour=Treatment, fill=Treatment), lty="dashed", alpha = 0.1, show_guide=FALSE) +
        theme_bw() +
        #geom_text(data=sites, aes(X,Y, label=Sample, color=Treatment), size=2, show_guide=FALSE) +
        theme(axis.line = element_line(colour = "black"),
              panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              #panel.border = element_blank(),
              panel.background = element_blank()) +
        coord_fixed() +
        annotate("text", x=-0.8, y=-0.55, label="Stress = 0.102")

p2 <- p1 +  geom_dl(data=sites, aes(X,Y,label=Sample, colour=Treatment, list( cex = 0.6)), method="smart.grid", show_guide=FALSE)
p2
Run Code Online (Sandbox Code Playgroud)

虽然这比在geom_text()行中使用vjust/hjust要好得多,但它仍然存在一些问题:

在此输入图像描述

例如,最左边三角形中的较低标签不必要地绘制在该区域上,并且绿色三角形中的一些标签(例如R53,R52)应放置在该区域之外.我在directlabels-package中尝试了很多选项,但是到目前为止,smart.grid是最好的方法.除了使用photoshop之外,我还能做些什么来改善标签吗?

这是我的数据:

sites <- structure(list(Sample …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

在 ggplot 2 2.0 中向 facet labeller 添加表达式

我需要用复杂的表达式替换一个 facet wrap 标签。但是,我无法再使用函数facet_wrap_labeller处理在较旧的 ggplot2 版本下创建的绘图。

        data <- as.data.frame(matrix(rnorm(60),20,3)) 
        data$factor <- c(rep("f1", 4),rep("f2", 4), rep("f3", 4), rep("f4",4),rep("f5", 4))
        names(data) <- c("A", "B", "C", "factor")
        melted <- melt(data)

        p <- ggplot(melted, aes(variable, value)) +
             stat_boxplot(geom ='errorbar') + 
             geom_boxplot()
        p1 <- p + facet_wrap(~factor)
        facet_wrap_labeller(p1, labels=c("A", "B", expression({}^2*italic(D)~textstyle(group("(", rarefied, ")")))))
Run Code Online (Sandbox Code Playgroud)

我得到:

Error in getGrob(gg[[strips[ii]]], "strip.text", grep = TRUE, global = TRUE) : 
  it is only valid to get a child from a "gTree"
Called from: getGrob(gg[[strips[ii]]], "strip.text", grep = TRUE, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

As_labeller与ggplot2 facet_wrap中的表达式()

library(ggplot2)
library(reshape)
df <- as.data.frame(matrix(runif(9),3,3))
df$factor <- letters[1:3]
df.m <- melt(df)
ggplot(df.m, aes(variable, value)) +
  geom_boxplot() +
  facet_wrap(~factor)
Run Code Online (Sandbox Code Playgroud)

我想改变方面名称.根据ggplot2教程,这是有效的:

new.lab <- as_labeller(c(a="A",b="B",c="C"))
ggplot(df.m, aes(variable, value)) +
  geom_boxplot() +
  facet_wrap(~factor, labeller=new.lab)
Run Code Online (Sandbox Code Playgroud)

但是,这不是:

new.lab <- as_labeller(c(a="A",b="B",c=expression(italic("C"))))
ggplot(df.m, aes(variable, value)) +
  geom_boxplot() +
  facet_wrap(~factor, labeller=new.lab)   
Run Code Online (Sandbox Code Playgroud)

我如何在ggplot2 2.0方面获得斜体(或任何其他特殊符号)?

r ggplot2

6
推荐指数
2
解决办法
2200
查看次数

按列名索引拆分数据框

这是早期问题的变体.

df <- data.frame(matrix(rnorm(9*9), ncol=9))
names(df) <- c("c_1", "d_1", "e_1", "a_p", "b_p", "c_p", "1_o1", "2_o1", "3_o1")
Run Code Online (Sandbox Code Playgroud)

我想通过下划线"_"后面的column.names中给出的索引来拆分数据帧.(索引可以是不同长度的任何字符/数字;这些只是随机的例子).

indx <- gsub(".*_", "", names(df))
Run Code Online (Sandbox Code Playgroud)

并相应地命名结果数据帧,最后我希望得到三个数据帧,称为:

  • df_1
  • df_p
  • df_o1

谢谢!

r

5
推荐指数
1
解决办法
2077
查看次数

igraph不对edge.width应用负相关系数

简单地说,我想在删除非重要值后根据相关强度绘制边.对于正相关对,我可以这样做edge.betweeness,但遗憾的是不能用于负数:

data <- matrix(rnorm(100),10,10)    
colnames(data) <- LETTERS[1:10]

library(Hmisc)
cor1 <- rcorr(data)
diag(cor1$r) <- 0

library(igraph)

#####Example 1:
graph <- graph.adjacency(cor1$r, weighted=TRUE, mode="lower")
graph <- delete.edges(graph, E(graph)[ weight < 0.3 & weight > -0.3])
#####trying to pass edge weights to edge.width
plot.igraph(graph, vertex.size=20, edge.width=E(graph)$weight)
###edge.width=E(graph)$weight is ignored

#####Example 2:
graph <- graph.adjacency(cor1$r, weighted=TRUE, mode="lower")
graph <- delete.edges(graph, E(graph)[ weight < 0.3]) #omitting the 2nd condition
E(graph)$weight <- edge.betweenness(graph) #apparently required
plot.igraph(graph, vertex.size=20, edge.width=E(graph)$weight)
####this does work, but only for …
Run Code Online (Sandbox Code Playgroud)

r igraph

5
推荐指数
1
解决办法
1369
查看次数

将共享图例与绘图网格的中心对齐(使用牛皮图)

可重复的示例可以在本教程中找到包牛皮图.

https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html

复制示例代码:

library(ggplot2)
library(cowplot)
#down-sampled diamonds data set
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]

# Make three plots.
# We set left and right margins to 0 to remove unnecessary spacing in the
# final plot arrangement.
p1 <- qplot(carat, price, data=dsamp, colour=clarity) +
  theme(plot.margin = unit(c(6,0,6,0), "pt"))
p2 <- qplot(depth, price, data=dsamp, colour=clarity) +
  theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("")
p3 <- qplot(color, price, data=dsamp, colour=clarity) +
  theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("")

# arrange the three plots in …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 cowplot

5
推荐指数
1
解决办法
2372
查看次数

多个图例的标题与ggplot2中的position =“bottom”不对齐

考虑这个情节:

library(scales)
library(ggplot2) 
df.m <- data.frame(Var1=c("A","B"), Var2=c("B", "A"), Similarity=c(97.5,92.5),
                       Rank=c("In", "Out"))

p <- ggplot(df.m, aes(Var1, Var2, fill=Similarity, col=Rank)) +
  geom_tile() +
  theme_bw()+
  theme(legend.position="bottom")+
  xlab("") +
  ylab("") +
  scale_fill_gradient2(low = muted("red"),
                       mid = "white", high = muted("blue")) +
  scale_color_manual(values=c("black", "gold")) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

图例的标题不一致。当我添加指南时,会发生这种情况:

p +   guides(color=guide_legend(override.aes = list(fill="white")),
         fill=guide_legend(title.hjust=0.5))
Run Code Online (Sandbox Code Playgroud)

标题现在对齐了,但填充图例的连续性丢失了。 在此输入图像描述

我能做些什么?

r ggplot2

5
推荐指数
1
解决办法
335
查看次数

限制geom_line的x轴范围(由斜率和截距定义)

library(ggplot2)
##
df <- as.data.frame(matrix(rnorm(60*2, mean=3,sd=1), 60, 2))
    colnames(df) <- c("A", "B")
    cf1 <- coef(lm(B~A, data=df))
##    
ggplot(df, aes(A,B)) +
  geom_point() +
  stat_smooth(method = "lm", color="red", fill="red", alpha=0.1, fullrange=TRUE) +
  #xlim(0,6)+
  geom_abline(intercept = cf1[1], slope = cf1[2], lty="dashed", col="green") 
Run Code Online (Sandbox Code Playgroud)

例

我想将geom_line限制为与stat_smooth相同的范围(似乎由xmax / xmin定义)。xlim参数没有帮助(这是在此处提出的)。在实际应用中,geom_line的斜率和截距将从模型更新中提取,因此它们会略有不同。谢谢。

r ggplot2

4
推荐指数
1
解决办法
2243
查看次数

标签 统计

r ×10

ggplot2 ×8

cowplot ×1

igraph ×1