在ggplot2中使用离散的x轴制作线图 [英] Making line plot with discrete x-axis in ggplot2

查看:358
本文介绍了在ggplot2中使用离散的x轴制作线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用刻面网格构建ggplot2图形.在我的Y轴上是百分比,而在X轴上是浓度(以数字表示).每个方面分为3组(0、24和48小时)

I am building a ggplot2 figure with a facet grid. On my Y-axis are percentages, and my X-axis is the concentration (in numbers). Each facet has 3 groups (0, 24 and 48 hours)

ggplot(data=MasterTable, aes(x=Concentration, y=Percentage, group=Time)) + 
  geom_point() + 
  geom_line() + 
  facet_grid(Chemicals ~ Treatments) 

这将生成一个连续的x轴.由于值分布不均,我希望使用离散轴更好地可视化我的数据.我遵循了后续教程,没有运气.第一个数字正是我正在尝试做的.

This generates a continuous x-axis. Since the values are not evenly spread out, I would prefer a discrete axis to better visualize my data. I followed the following tutorial with no luck. The first figure is exactly what I am trying to do.

我也尝试格式化轴:

scale_x_discrete(labels("0", "0.1", "2", "50"))

并设置行格式:

geom_line(aes(Time))

并遵循我认为这个问题是x轴上的值是整数而不是字符串.这使默认轴连续.我该如何更改?我确信解决方案很简单,我只是想不通.

I think this problem is that the values on the x-axis are integers rather than strings. This makes the default axis continuous. How can I change this?? I am sure the solution is simple, I just can't figure it out.

提前谢谢!

推荐答案

在此

On this page they make the following modification df2$dose<-as.factor(df2$dose). You can try to modify your x-axis as df2$Concentration<-as.factor(df2$Concentration)

或类似这样:

ggplot(data=MasterTable, aes(x=factor(Concentration), y=Percentage, group=Time)) + 
  geom_point() + 
  geom_line() + 
  facet_grid(Chemicals ~ Treatments) 

这篇关于在ggplot2中使用离散的x轴制作线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆