如何使用另一个文件(R)中的数据将数据合并到在ggplot2中构建的图中?

需要帮助2

使用数据集,我创建了以下图:

在此处输入图片说明

我正在尝试创建以下情节:

在此处输入图片说明

具体来说,我正在尝试在第一张图片上合并Twitter名称。为此,我有一个数据集,每个名称都在其中,并且有一个与轴上的点相对应的值。摘要如下所示:

Name             Score
@tedcruz         0.108
@RealBenCarson   0.119

有谁知道如何在原始图(由另一个CSV文件中的数据构成)上绘制这些数据(来自一个CSV文件)?我感到困惑的原因是,在中ggplot2,您一开始就指定了要使用的数据,所以我不确定如何合并其他数据。

谢谢。

多雷米

The question you ask about ggplot combining source of data to plot different element is answered in this post here

Now, I don't know for sure how this is going to apply to your specific data. Here I want to show you an example that might help you to go forward.

Imagine we have two data.frames (see bellow) and we want to obtain a plot similar to the one you presented.

data1 <- data.frame(list(
  x=seq(-4, 4, 0.1), 
  y=dnorm(x = seq(-4, 4, 0.1))))
data2 <- data.frame(list(
  "name"=c("name1", "name2"), 
  "Score" = c(-1, 1)))

The first step is to find the "y" coordinates of the names in the second data.frame (data2). To do this I added a y column to data2. y is defined here as a range of points from the may value of y to the min value of y with some space for aesthetics.

range_y = max(data1$y) - min(data1$y)
space_y = range_y * 0.05
data2$y <- seq(from = max(data1$y)-space, to = min(data1$y)+space, length.out = nrow(data2))

Then we can use ggplot() to plot data1 and data2 following some plot designs. For the current example I did this:

library(ggplot2)
p <- ggplot(data=data1, aes(x=x, y=y)) + 
  geom_point() + # for the data1 just plot the points
  geom_pointrange(data=data2, aes(x=Score, y=y, xmin=Score-0.5, xmax=Score+0.5)) +
  geom_text(data = data2, aes(x = Score, y = y+(range_y*0.05), label=name))
p 

which gave this following plot:

带有两个数据的示例ggplot

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将一个模型的数据合并到另一个模型中?

将数据集中的同一列合并到R中另一个列的所有列上?

使用glob后如何将数据帧合并到一个csv文件中?

通过匹配r中的值和列名,将一个数据框的值合并到另一个数据框

将许多XML文件合并到R中的一个数据帧中

将部分数据库表合并到另一个数据库表中?

在ggplot2中,如何在图中添加另一个y轴?

使用setDT将一个数据帧中的许多列合并到另一数据帧中

如何有效地将具有MultiIndex的数据帧合并到另一个数据帧中?

将两个表中的数据合并到一个视图中

如何将2组数据合并到一个UITableview中

如何在R中使用ggplot2和gridExtra包将一个grob对象布置在另一个grob对象中

将存储库合并到另一个存储库的文件夹中

如何将各种csv文件中的数据合并到python中的一个csv文件中?

如何查看3个不同的列以将一个公共数字与另一个数据框的一个列进行匹配以合并到数据中(如果没有匹配项追加)?

将多个CSV文件中的列数据合并到一个CSV文件中

如何使用python将Excel文件的所有行和列合并到另一个Excel文件的单个单元格中?

合并多个文本文件数据,但将特定的行合并到另一个

将响应数据合并到一个响应中

将R中的两个列表合并到一个数据框中

在python熊猫中,如何合并两个数据帧,同时使用另一个的权重将值散布在一个数据中?

将一个熊猫数据帧合并到另一个熊猫数据帧,并从第二个数据帧中删除第一个数据帧中存在的值

pandas concat 将数据帧列表合并到另一个数据帧

将2个AJAX调用中的数据合并到一个数组中?

如何将一个文件合并到另一个\ Linux

将一个分支合并到另一个分支的文件夹中

在 Django 中,如何使用基于函数的视图中的另一个模型数据

如何将 Pandas DataFrame 中的数据与多索引合并到一个列表中

将一个元组序列合并到Scala中的另一个序列