简体   繁体   English

如何在指定 x 轴标签的顺序时使用 ggplot2 在一个图上绘制两条线?

[英]How to plot two lines on one plot with ggplot2 while specifying order of the x-axis labels?

The data are in "stats":数据在“统计”中:

run       valence mean sd sem
Nnf1_mINS   n     2.1 .5 .01
Nnf2_mINS   p     3.2 .2 .01
Nobs_mINS   n     2.3 .1 .02

Here's my code:这是我的代码:

ggplot(stats,aes(x=run,y=mean,color=valence,group=1))+ 
    geom_point() +
    geom_line()+
    geom_errorbar(aes(ymin = mean - sem, ymax = mean + sem),                         
                  width = 0.2,
                  position = position_dodge(0.9))

3 problems: 3个问题:

  1. to specify the order on x axis, I turned the variable into a factor:为了指定 x 轴上的顺序,我将变量变成了一个因子:
stats$run <- factor(stats$run, levels = c("Nobs_mINS","Nnf1_mINS","Nnf2_mINS"))
  1. but to plot 2 lines on one plot, there can't be a factor variable.但是要在一个图上绘制 2 条线,不能有因子变量。 This is the error: "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?"这是错误:“geom_path:每个组仅包含一个观察值。您需要调整组审美吗?” I fixed this error by adding "group=1" thanks to this post: ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"由于这篇文章,我通过添加“group=1”修复了这个错误: ggplot2 折线图给出了“geom_path:每个组只包含一个观察值。你需要调整组美学吗?”

  2. For some reason the line is not connected correctly:由于某种原因,线路未正确连接: 在此处输入图像描述

Well, group = valence solves the problem!好吧,group = valence 解决了这个问题! Here's the edited code:这是编辑后的代码:

ggplot(stats,aes(x=run,y=mean,color=valence,group=valence))+ 
    geom_point() +
    geom_line()+
    geom_errorbar(aes(ymin = mean - sem, ymax = mean + sem),                         
                  width = .2,
                  position = position_dodge(.09))+
    theme(text = element_text(size=15),
          axis.text.x = element_text(angle=0, hjust=1))+
    scale_y_continuous(limits = c(-0.22, 0.1))

the graph:图表: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在保留 x 轴标签的同时抑制 ggplot2 图中的垂直网格线? - How can I suppress the vertical gridlines in a ggplot2 plot while retaining the x-axis labels? 如何在ggplot2条形图中的刻度线之间绘制x轴标签和条形图? - How to plot x-axis labels and bars between tick marks in ggplot2 bar plot? ggplot2 的小提琴 plot 与 x 轴数据集中的顺序不同 - Violin plot of ggplot2 is not in order as in dataset on x-axis 如何在ggplot2散点图中的每个点下方添加x轴刻度线和标签 - How to add x-axis ticks and labels bellow every point in a ggplot2 scatter plot 如何在 ggplot2 中在 x 轴上绘制两个向量,在 y 轴上绘制另一个向量 - How do you plot two vectors on x-axis and another on y-axis in ggplot2 ggplot2如何将行绘制到多个x轴数据点 - ggplot2 how to plot rows to multiple x-axis datapoints 使用Cowplot将其他x轴标签添加到ggplot2绘图(带有离散轴标签) - Add additional x-axis labels to a ggplot2 plot (with discrete axis labels) using cowplot 如何在ggplot2中创建密度图,其中x轴为日期 - How to make a density plot in ggplot2 with dates on the x-axis 如何使用ggplot2包将X轴上的2个分类变量和两个连续变量绘制为“填充”? - How to plot 2 categorical variables on X-axis and two continuous variables as “fill” using ggplot2 package? R ggplot2-在一个具有不同x轴范围的图中绘制多个函数 - R ggplot2 - Plot multiple functions in one plot with different x-axis ranges
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM