小编Tje*_*ebo的帖子

对于具有不同小平面编号和coord_fixed?的绘图获得相同的高度?

让我在图片中解释我的意思:

set.seed(1)  ## dummy data.frame:
df <- data.frame( value1 = sample(5:15, 20, replace = T), value2 = sample(5:15, 20, replace = T),
                  var1 = c(rep('type1',10), rep('type2',10)), var2 = c('a','b','c','d'))

## Plot 1 

ggplot() +
  geom_point(data = df, aes(value1, value2)) +
  facet_grid(~var1) +
  coord_fixed()

ggsave("plot_2facet.pdf", height=5, units = 'in')
    #Saving 10.3 x 5 in image

## Plot 2  which I want to save in a separate file (!)

ggplot() +
  geom_point(data = df, aes(value1, value2)) +
  facet_grid(~var2) +
  coord_fixed()

ggsave("plot_4facet.pdf", height=5, …
Run Code Online (Sandbox Code Playgroud)

pdf plot r facet ggplot2

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

扩展轴限制而不作图(以x单位对齐两个图)

我正在尝试将两个ggplot对象与patchwork-具有不同数据子集的两个图结合在一起,但它们具有相同的x变量(因此具有相同的单位)。我想根据x值对齐图- 每个x单位在最终图中应具有相同的物理宽度

实际上绘制较大数据集的整个宽度时,这非常容易(请参见下图)-但我很难仅绘制部分数据并保持相同的对齐方式。

library(ggplot2)
library(patchwork)
library(dplyr)

p1 <- 
ggplot(mtcars, aes(mpg)) + 
  geom_density(trim = TRUE) +
  scale_x_continuous(limits = c(10,35))

p2 <- 
ggplot(filter(mtcars, mpg < 20), aes(mpg)) + 
  geom_histogram(binwidth = 1, boundary = 1) +
  scale_x_continuous(limits = c(10,35)) 

p1/p2
Run Code Online (Sandbox Code Playgroud)

reprex软件包(v0.3.0)创建于2019-08-07

所需的输出
这是Photoshop处理

在此处输入图片说明 添加coord_cartesian(xlim = c(10,(20 or 35)), clip = 'off')和/或更改scale_x限制c(0,(20 or 35))无效。

patchwork当它们处于两行时,也不会让我设置两个图的宽度,这在某种意义上是有意义的。因此,我可以为第二行创建一个空图并为其设置宽度,但这似乎是很糟糕的事情,我觉得必须有一个更简单的解决方案。
我不限于patchwork,但是任何允许使用它的解决方案都将受到欢迎。

r ggplot2

7
推荐指数
2
解决办法
178
查看次数

设置相对于n的重叠区域的灰度值的不透明度

我很难理解(和控制)alpha的混合-不幸的是,alpha值并不能简单地“累加”(0.5 + 0.5不是1)。但是我怎么能做到呢?

目的是定义相对于观测总数的重叠区域的(绝对)灰度值。请参见下面的示例。

我试图将其设置scale_alpha(range = c(0,1))为无济于事,也许我没有正确使用它。

library(ggplot2)
library(ggforce)

grid_df = data.frame(x = c(1:2, 2.5), y = rep(1,3), r = 1)

ggplot()+
geom_circle(data = grid_df, mapping = aes(x0 = x,  y0 = y, r = r), alpha = 0.33, fill = 'black') + 
  coord_fixed() 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

alpha r ggplot2

7
推荐指数
3
解决办法
173
查看次数

带有分隔符而不是刻度的连续颜色条

图片应该很好地说明了我想要实现的目标。理想情况下,生成的颜色条将保持连续性,即应该基于连续而不是离散的美学,或者至少非常接近于表现得像它。

这也意味着,不仅仅是将离散填充的图例键靠得足够近的简单方法。

是的,我可以创建一个假的 legend,但我想要一个在图例绘图级别进行修改的解决方案。

我也会很高兴有趣的网格技巧!

library(ggplot2)

ggplot(iris, aes(Sepal.Length, y = Sepal.Width, fill = Petal.Length))+
  geom_point(shape = 21) +
  scale_fill_continuous() +
  guides(fill = guide_colorbar(ticks.colour = "black"))
Run Code Online (Sandbox Code Playgroud)

那是photoshopped,道歉!我不想创建箭头,只是使用分隔符而不是刻度的连续比例 在此处输入图片说明

最终目标是重现此线程中最初提出的颜色条 在此处输入图片说明

然而,这不是关于创建离散梯度条,这只是关于分隔符!

离散渐变栏是没有问题的,也有已经在那里的解决方案(例如,/sf/answers/4378108381//sf/answers/4378973441/https://开头stackoverflow.com/a/50540633/7941188 )

r legend ggplot2

7
推荐指数
2
解决办法
375
查看次数

注册的 S3 方法被“GGally”覆盖:来自 +.gg 的方法 - RStudio 加载包而不运行代码?

GGally 中的行为非常奇怪 -我在这里提出了一个问题。我想问你们的是,你们认为这是 RStudio 还是 GGally 中的错误吗?这是怎么发生的?

在一个全新的 R Studio 项目中,当打开包含一行具有 GGally 函数的代码的脚本时(在我的例子中,打开一个仅包含一行的脚本GGally::ggcorr(p),仅此而已),控制台中会弹出一条消息。

注册的 S3 方法被“GGally”覆盖:来自
+.gg ggplot2 的方法

甚至没有运行一行代码就会发生这种情况!

不知何故,GGally 似乎被加载到命名空间中,但仅在 RStudio 中,而不是在运行 reprex 时加载。此外 - 仅当我使用 调用函数时才会发生这种情况GGally::...,并且例如在以下情况下不会发生

library(GGally)
ggcorr(p)
Run Code Online (Sandbox Code Playgroud)

每当在 RStudio 中使用包含以下内容的已保存脚本重新启动 R 时,都会出现此消息GGally::...

SessionInfo(没有运行实际代码!)

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         GGally_2.1.1       magrittr_2.0.1     tidyselect_1.1.1  
 [5] munsell_0.5.0      colorspace_2.0-0   R6_2.5.0           rlang_0.4.11      
 [9] fansi_0.4.2        plyr_1.8.6         dplyr_1.0.5        tools_4.0.5       
[13] grid_4.0.5         gtable_0.3.0       utf8_1.2.1         DBI_1.1.1         
[17] ellipsis_0.3.2     yaml_2.2.1         assertthat_0.2.1   tibble_3.1.1      
[21] lifecycle_1.0.0    crayon_1.4.1 …
Run Code Online (Sandbox Code Playgroud)

r rstudio ggally

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

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

我正在尝试创建一个结合了 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
查看次数

如何使用 gganimate 动画让 x 轴跨度移动?

使用 R,我尝试使用 gganimate 制作一个基于 x 轴从左到右显示的折线图。我已经设法做到了这一点,但我还想做的是使scale_x_continuous(limits = c(i-5,i+5)),即在正在显示的点和窗口周围有一个窗口将继续前进,同时揭示下一点。

我尝试了很多方法来实现这一点,包括在带有和不带有 aes() 的scale_x_continuous 中实现某种循环。似乎什么都不起作用。我对 ggplot2 特别是 gganimate 很陌生,但我在网上找不到任何帮助。我有一种感觉,答案可能很简单,但我只是错过了。

有点像这样,但使用 gganimate:

类似的例子,但不是 gganimate

以下是一些可重现的代码,向您大致展示我到目前为止所做的事情。

library(ggplot2)
library(gganimate)
library(gifski)
library(png)


Step  <- c(1:50,1:50)
Name  <- c(rep("A",50), rep("B",50))
Value <- c(runif(50,0,10), runif(50,10,20))
Final <- data.frame(Step, Name, Value)

a <- ggplot(Final, aes(x = Step, y = Value, group = Name, color = factor(Name))) + 
 geom_line(size=1) + 
 geom_point(size = 2) + 
 transition_reveal(Step) + 
 coord_cartesian(clip = 'off') + 
 theme_minimal() +
 theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
 theme(legend.position = "none") 

options(gganimate.dev_args …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

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

How do you keep the first value in multiple columns?

I have a dataframe. I want to keep the first value in multiple columns.

I prefer a tidyverse solution using the pipe %>% operator. I prefer a solution where I can input E1:N3 because I have about 50 columns in the real dataset.

This is the dataframe:

df <- data.frame(age = c(20, 25, 30), E1 = c("1 Alpha", "2 Bravo", "1 Alpha"), E2 = c("2 Bravo", "2 Bravo", "2 Bravo"), E3 = c("1 Alpha", "2 Bravo", "2 Bravo"), N1 = …
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

如何填充由直线和曲线创建的几何图形?

我试图用不同的颜色填充下图中的 3 个三角形。

这是图形

data = data.frame(x=c(125), y=c(220)) #this data is just to be able to use gplot to draw figures

ggplot(data, aes(x = x, y = y)) + 
  xlim(0,250) +
  ylim(-250, 0) +
  geom_curve(x = 33, xend = 223, y = -100, yend = -100, curvature = -.65) +
  geom_segment(x=128, xend = 33, y=-208, yend = -100) +
  geom_segment(x=128, xend = 223, y=-208, yend = -100) +
  geom_segment(x=128, xend = 159.67, y=-208, yend = -45) +
  geom_segment(x=128, xend = 96.33, …
Run Code Online (Sandbox Code Playgroud)

r fill ggplot2

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

如何在 r 中为 ggplot 自定义调色板?

我使用此代码绘制北美温度变化地图:

ggplot(maps, aes(y=Latitude, x=Longitude, z=variable)) +
  ggtitle(title)+
  scale_fill_brewer(palette = "Spectral")+
  geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))+
  xlim(-146,-44)+
  ylim(35,90)+
  theme(plot.title = element_text(hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)

我想更改调色板以获得更好的可视化效果。目前,它写道 scale_fill_brewer(palette = "Spectral"),我将把它更改为自定义调色板

colors <- c(rgb(10,40,100,max=255),rgb(51,102,217,max=255),rgb(105,138,236,max=255),rgb(151,180,250,max=255),rgb(204,217,255,max=255),rgb(255,245,204,max=255),rgb(255,224,153,max=255),rgb(255,203,102,max=255),rgb(255,180,51,max=255),rgb(255,140,51,max=255),rgb(255,85,0,max=255),rgb(230,40,30,max=255),rgb(191,0,0,max=255),rgb(140,0,0,max=255),rgb(108,0,0,max=255),rgb(110,0,70,max=255))
Run Code Online (Sandbox Code Playgroud)

使用scale_fill_brewer(palette = colors)会报错,我也试过了palette(colors),也是不行。

如何自定义参数可以识别的调色板?

r palette scale ggplot2

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

标签 统计

r ×10

ggplot2 ×8

alpha ×1

cowplot ×1

dplyr ×1

facet ×1

fill ×1

ggally ×1

gganimate ×1

legend ×1

palette ×1

pdf ×1

plot ×1

rstudio ×1

scale ×1