English 中文(简体)
从多种数据框架中生成综合定制数据
原标题:Create from multiple data frames complex customized figure on ggplot2

I am working on a project where I need to combine data from two different data frames into a single plot using ggplot2 in R. I was wondering how I can efficiently achieve this, assigning specific features to each dataset in the plot.

我有两个数据框架,每个框架都有不同的变量。 我愿在单一图表中为df1和df2铺设地块,同时为每个数据集提供彩色、形状和标签等定制地块。

我希望我的阴谋能够突出两个背景信息,一个是纯洁的,另一个是绿色的,另一个是每个斜体的roid。 这些计算方法是利用<代码>stat_ellipse和df2的数据生成的。 就地而言,我想到df1>中的要点,但如以下文字所述,这些要点有色梯度:

# Create data frame df1
df1 <- data.frame(
  x = c(1, 2, 3, 4, 5),
  y = c(2, 4, 3, 6, 5),
  color = runif(5, 0, 1))


# Create data frame df2
df2 <- data.frame(
  x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
  y = rnorm(10),
  group = rep(c( A ,  B ), each = 5))

为了造就这块土地,我不敢确定该信息是否应当直接输入到综合法典中,或者说应该直接输入地球点和静态。 无论是哪种方式,我都无法保证如何分别调整每一块地的颜色。

plotGG <- ggplot() +
  stat_ellipse(data = df2, aes(x = x, y = y, color = group)) + #i want those ellipses purple and green
  geom_point(data = df1, aes(x = x, y = y, color = color), size = 3) +
  scale_color_gradient2(midpoint=0.5,low="#ba1414ff",mid = "#f3f3b9ff",high="#369121ff") #this is the color pallete that i want for the geom_points

I have progressed to this point, but it generates errors one way or another. Thank you in advance!

问题回答

In vanilla ggplot2 你们只能有每个美学(因为你想要两个不同的彩色比额表)的规模,要么是分散的,要么是连续的(因为你想要一个单独和连续的彩色比额表)。

但是,实现您预期结果的一种选择是ggnewpl的一揽子计划,允许同一假设的多个比额表:

set.seed(123)

# Create data frame df1
df1 <- data.frame(
  x = c(1, 2, 3, 4, 5),
  y = c(2, 4, 3, 6, 5),
  color = runif(5, 0, 1)
)

# Create data frame df2
df2 <- data.frame(
  x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
  y = rnorm(10),
  group = rep(c("A", "B"), each = 5)
)

library(ggplot2)
library(ggnewscale)

ggplot() +
  stat_ellipse(
    data = df2,
    aes(x = x, y = y, color = group)
  ) +
  scale_color_manual(
    values = c(A = "purple", B = "green")
  ) +
  ggnewscale::new_scale_color() +
  geom_point(
    data = df1,
    aes(x = x, y = y, color = color), size = 3
  ) +
  scale_color_gradient2(
    midpoint = 0.5,
    low = "#ba1414ff", mid = "#f3f3b9ff", high = "#369121ff"
  )

></p>
    </div>
       </div>
            <footer class=





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...