小编bde*_*est的帖子

ggplot2 y轴标签小数精度

我在一个循环中绘制了几个ggplot图表(我知道,我知道不要循环使用plyr ...但是)并且很奇怪是否有办法将小数精度设置为一个小数(即0.0).我正在使用以下比例转换.

p <- p + scale_y_log10()
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

r ggplot2

20
推荐指数
1
解决办法
3万
查看次数

绘制正态分布

我试图绘制3个正态分布的例子,但ggplot似乎将路径识别为一个连续的路径,而不是按因子水平分层的路径.我对ggplot比较新,任何帮助都会非常感激.

这是我的代码:

set.seed(5872)

x<-seq(-7.5,7.5,0.1)
l<-length(x)*3
df<-data.frame(P=factor(rep(c("Mean: -1, SD: 0.5","Mean: 0, SD: 1","Mean: 1, SD: 1.5"),      each=l) ),
X=(c(x,x,x)), 
Y=(c(dnorm(x,-1,0.5),dnorm(x,0,1),dnorm(x,1,1.5))))

Normal<-ggplot(data=df,aes(X,Y,group=P,color=P))+
geom_path()+
scale_x_continuous("")+
scale_y_continuous("f(x)")+
scale_color_discrete("Parameters")+
ggtitle("Normal") + 
theme(plot.title = element_text(size=25,lineheight=.8, face="bold"))
Run Code Online (Sandbox Code Playgroud)

如何让ggplot识别这些因素并用3种不同颜色绘制?而不是显示一条连续路径?

r distribution normal-distribution ggplot2

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

具有共同图例的 ggarrange 在降价中产生额外的空白图

我正在使用 R Notebooks 并且有一个包含一些用于排列直方图的代码的块。当我使用一个普通的图例时,它会产生一个额外的空图,在渲染的 html 文件中看起来很糟糕。这种现象在没有共享图例的情况下消失了,但由于它们的大小不同,因此情节看起来很糟糕。有什么办法可以阻止它产生额外的空图?

笔记本块输出

和块中使用的代码

 ```{r}

ggarrange(

gghistogram(data, title="MOTIVATION SCORES", x="MOTIVATION", y="..density..", 
add_density=TRUE, add = "median", rug = TRUE, bins=15, color="#69c8ECFF", 
fill="#69c8ECFF") , 


gghistogram(data, title="MOTIVATION BY AGE GROUP", x = "MOTIVATION", 
y="..density..", add_density=TRUE,
          add = "median", rug = TRUE, bins=15,
          color = "AGE_GROUP", fill = "AGE_GROUP",
          palette = c("#69c8ECFF", "#E762D7FF")
          )

, legend = "bottom" 
, common.legend = TRUE

)
```
Run Code Online (Sandbox Code Playgroud)

r ggplot2 r-markdown rnotebook ggpubr

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

在 ggplot2 中绘制大矩阵的直方图比基本 hist() 慢 20 倍

我有一个数字矩阵,大约有 10M 个值,只需要在直方图中显示值的分布。在基础 R 中,hist()这样做的速度非常快。但是如果我想使用ggplot,它慢得多(我还必须先融化矩阵,但这不是限时步骤)。有什么办法可以用 ggplot 使它快速吗?

require(microbenchmark)
require(ggplot2)


mtx1 <- matrix(rnorm(6e4*150), nrow = 6e4)
df1 <- reshape2::melt(mtx1)

g_hist <- function(df){
  print(ggplot(df, aes(x=value)) + geom_histogram(bins=30))
}

print(microbenchmark(
  hist(mtx1), 
  g_hist(df1), 
times=3L 
), signif=3)


# Unit: milliseconds
#        expr  min   lq mean median   uq  max neval
#  hist(mtx1)  384  471  530    559  603  647     3
# g_hist(df1) 7710 8000 8190   8300 8440 8570     3
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

ggplot 中带有 facet 的变量 hline

以 Iris 数据集为例,我可以生成一个带有 facet 的 ggplot。 ggplot 与 facet 代码是:

library(ggplot2)
data(iris)
y=iris
y$Petal.Width.Range=factor(ifelse(y$Petal.Width<1.3,"Narrow","Wide"))
y$Petal.Length.Range=factor(ifelse(y$Petal.Length<4.35,"Short","Long"))
ggplot(y, aes(Sepal.Length,Sepal.Width)) + 
  geom_point(alpha=0.5)+
  geom_hline(yintercept =3 ,alpha=0.3)+
  facet_grid(Petal.Width.Range ~ Petal.Length.Range)
Run Code Online (Sandbox Code Playgroud)

在这里,我在 4 种情况下的水平规格均为 3。如果我想要一个依赖于案例的规范,我该怎么办?例如,我可以定义 4 个不同的规范,如下所示:

y$threshold=2
y$threshold[(y$Petal.Width.Range=="Narrow")&(y$Petal.Length.Range=="Short")] =2
y$threshold[(y$Petal.Width.Range=="Narrow")&(y$Petal.Length.Range=="Long")] =2.5
y$threshold[(y$Petal.Width.Range=="Wide")&(y$Petal.Length.Range=="Short")] =3.1
y$threshold[(y$Petal.Width.Range=="Wide")&(y$Petal.Length.Range=="Long")] =4
Run Code Online (Sandbox Code Playgroud)

我应该如何将 y$threshold 添加到 ggplot 命令中?

r facet ggplot2

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

我正在尝试在R中重新创建一个特定的图(ggplot2)

尽管已尝试过多种类型的线,但我无法得到相同的结果.以下是我需要线条的方式:

原始情节

这就是我到目前为止得到它的方式(我坚持): 我的情节版本

这是我的代码:

myData <- read.csv(file.choose(), header = TRUE)
require(ggplot2)
g <- ggplot(myData, aes(speed, resp))
g + geom_point(aes(color = padlen, shape = padlen)) +
geom_smooth(method = "lm", formula = y ~ splines::bs(x, df = 4, degree = 2), se = FALSE, aes(color = padlen), linetype = "solid", size = 1) +
scale_color_manual(values = c("red", "black")) +
scale_shape_manual(values = c(2, 1))
Run Code Online (Sandbox Code Playgroud)

这是数据库(dput):

myData <- structure(list(resp = c(0, 0.125, 0.583333333, 1, 0.958333333, 
1, 0, 0.041666667, 0.25, 0.916666667, 1, 1), padlen = structure(c(2L, …
Run Code Online (Sandbox Code Playgroud)

r lines spline ggplot2

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

带有数学表达式的ggplot2 facet_wrap

使用facet_wrap(),我想用数学表达式标记每个单独的图:

library(ggplot2)

x       <- rnorm(100, 50, 10)
y       <- rnorm(100, 50, 10)
grp     <- rep(c("a", "b", "c", "d"), each=25)
test.df <- data.frame(grp, x, y)
mean.df <- aggregate(test.df[c("x", "y")], test.df["grp"], mean)

p <- ggplot(test.df) +
     geom_point(aes(x=x, y=y, col=grp)) + 
     facet_wrap(~ grp) +
     geom_text(data=mean.df, aes(x=x, y=y, label=paste("xbar=", round(x,1))))
p
Run Code Online (Sandbox Code Playgroud)

我想要\ bar(x)而不是xbar.我尝试了表达式(),但我得到:"不能强制类""表达式""到data.frame".

math r ggplot2 facet-wrap

3
推荐指数
1
解决办法
284
查看次数

为什么我的R代码生成多行和条形图这么慢?

我正在努力制作一些相当复杂的情节,它们结合了R中的几个数据集,ggplot2这对于这项工作非常有用,但是人类很慢.我意识到我正在处理大量的数据点,但我认为我在任何地方都有一个任意的瓶颈.让我解释...

我有10个不同的向量,每个150,000个条目长.我想使用ggplot2在命令行上创建一个带有这些的图形,并将生成的png保存到磁盘.10个矢量中的每一个将是不同的颜色,一些将是线条,一些将是条形.代码如下所示:

bulk = data.frame(vector1=c(1,5,3,5,...), ... vector10=c(5,3,77,5,3, ...))
png(filename="figure.png", width=4000, height=800)
ggplot(bulk, aes(x=vector1), aes(alpha=0.2)) +
geom_bar(aes(y=vector2), color="red", stat="identity") +
geom_bar(aes(y=vector3), color="black", stat="identity") +
..................
geom_line(aes(y=vector10), color="black", size=1) +
scale_y_log10()
Run Code Online (Sandbox Code Playgroud)

请记住,我有10个向量,每个150,000个条目长,所以我有1.5M数据点来绘制.但是,我使用的是具有32GB RAM的8核4Ghz /核心机器,但是R几乎没有RAM,只有1个核心.这是预料之中的,因为据我所知这个过程不能是多线程的,但渲染真的需要每个数字大约1个小时吗?

感觉就像我的代码是任意膨胀这个处理时间.特别是因为每10个矢量20,000个条目的相同问题仅需要大约20秒.将其缩放比线性缩放时间更重要.

有没有人对这个问题有解决方案或怀疑?谢谢你的帮助!

plot r ggplot2

2
推荐指数
1
解决办法
2226
查看次数

在ggplot2中的geom_bar上堆叠多个列

实质上:

  1. 我想绘制一个条形图,显示两个表列的聚合值,我已经设法使用: err.bar <- ggplot(ss.data, aes(x=pop, y=obs+proc)) err.bar <- err.bar + geom_bar(position="stack", stat = "identity") err.bar

  2. 我想要聚集,不一定是颜色,聚合条的两个部分.

  3. 最后,我想通过根据物种对它们进行分组来对颜色进行着色(即,在Excel图表的x轴标签上指示的物种E&C)

我使用的数据类似于:

  • pop E1 E2 E3 E4 E5 E6 E7 C1 C2 C3 C4
  • obs 0.0027 0.0018 0.0464 0.0095 0.0034 0.0117 0.017 0.1178 0.0449 0.039 0.0903
  • proc 0.0319 0.0196 0.0511 0.0143 0.0048 0.0078 0.0396 0.1662 0.074 0.1681 0.1358

r ggplot2

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

将 geom_smooth 中的 nls 函数与 ggplot2 结合起来

我有以下问题!\n我使用以下代码使用 ggplot2 创建一个图形:

\n\n
bi28 <- ggplot(Loc28, aes(patch, biovmean, shape=group,colour=group,group=id)) +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 geom_errorbar(aes(ymin=biovmean- biovse,ymax=biovmean+biovse),\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 colour="black",width=.4,position=pd,size=.7) +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 geom_point(size=5,position=pd) +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 ylab(expression(paste(Log~biovolume~"[",\xc2\xb5m^3~mL^-1,"]"))) +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 xlab("Patch-Position") +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 theme_bw() +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 scale_colour_manual(name="Nutrient supply, Dispersal",\xc2\xa0\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 labels=c("Grad, High","Grad, Low", "Uniform, High", "Uniform, Low"),\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 values=c("black","darkgrey","black","darkgrey")) +\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 scale_shape_manual(name="Nutrient supply, Dispersal",\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 labels=c("Grad, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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