热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Rggplot2中使用scale_x_discrete

如何解决《在Rggplot2中使用scale_x_discrete》经验,为你挑选了1个好方法。

我在使用R中的ggplot2中的离散比例时遇到问题

g + scale_x_discrete(breaks=1:7, labels=1:7)

错误地更改了图表的限制.

之前:

之前

后: 在此输入图像描述

我看不出有什么可疑的用于生成图像的代码,但是这里是:

g <- ggplot(data=plottingData, aes(x=x, y=y, ymin=ymin, ymax=ymax)) +
geom_bar(stat="identity", fill=col) +
geom_errorbar(width=0.5*binwidth, size=0.3)

这是dput()生成的ggplot2对象:

structure(list(data = structure(list(x = c(1, 2, 3, 4, 5, 6,  7), y = c(0.689655172413793, 0.689655172413793, 11.0344827586207,  2.75862068965517, 70.3448275862069, 13.7931034482759, 0.689655172413793 ), ymin = c(0, 0, 6.84765916431683, 0.870298113507349, 62.426550974053,  9.06894448064895, 0), ymax = c(4.26873021234759, 4.26873021234759,  17.3134146611865, 7.18339316166044, 77.1707644621886, 20.4612568616329,  4.26873021234759)), .Names = c("x", "y", "ymin", "ymax"), row.names = c(NA,  -7L), class = "data.frame"), layers = list(, ),      scales = ,      mapping = structure(list(x = x, y = y, ymin = ymin, ymax = ymax), .Names = c("x",      "y", "ymin", "ymax"), class = "uneval"), theme = structure(list(         line = structure(list(colour = "black", size = 0.5, linetype = 1,              lineend = "butt"), .Names = c("colour", "size", "linetype",          "lineend"), class = c("element_line", "element")), rect = structure(list(             fill = "white", colour = "black", size = 0.5, linetype = 1), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), text = structure(list(family = "", face = "plain",              colour = "black", size = 12, hjust = 0.5, vjust = 0.5,              angle = 0, lineheight = 0.9), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          axis.text = structure(list(family = NULL, face = NULL,              colour = NULL, size = structure(0.8, class = "rel"),              hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          strip.text = structure(list(family = NULL, face = NULL,              colour = NULL, size = structure(0.8, class = "rel"),              hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          axis.line = structure(list(), class = c("element_blank",          "element")), axis.text.x = structure(list(family = NULL,              face = NULL, colour = NULL, size = NULL, hjust = NULL,              vjust = 1, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          axis.text.y = structure(list(family = NULL, face = NULL,              colour = NULL, size = NULL, hjust = 1, vjust = NULL,              angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          axis.ticks = structure(list(colour = "black", size = NULL,              linetype = NULL, lineend = NULL), .Names = c("colour",          "size", "linetype", "lineend"), class = c("element_line",          "element")), axis.title.x = structure(list(family = NULL,              face = NULL, colour = NULL, size = NULL, hjust = NULL,              vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          axis.title.y = structure(list(family = NULL, face = NULL,              colour = NULL, size = NULL, hjust = NULL, vjust = NULL,              angle = 90, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          axis.ticks.length = structure(0.15, unit = "cm", valid.unit = 1L, class = "unit"),          axis.ticks.margin = structure(0.1, unit = "cm", valid.unit = 1L, class = "unit"),          legend.background = structure(list(fill = NULL, colour = NA,              size = NULL, linetype = NULL), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), legend.margin = structure(0.2, unit = "cm", valid.unit = 1L, class = "unit"),          legend.key = structure(list(fill = NULL, colour = "grey80",              size = NULL, linetype = NULL), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), legend.key.size = structure(1.2, unit = "lines", valid.unit = 3L, class = "unit"),          legend.key.height = NULL, legend.key.width = NULL, legend.text = structure(list(             family = NULL, face = NULL, colour = NULL, size = structure(0.8, class = "rel"),              hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          legend.text.align = NULL, legend.title = structure(list(             family = NULL, face = "bold", colour = NULL, size = structure(0.8, class = "rel"),              hjust = 0, vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          legend.title.align = NULL, legend.position = "right",          legend.direction = NULL, legend.justification = "center",          legend.box = NULL, panel.background = structure(list(             fill = "white", colour = NA, size = NULL, linetype = NULL), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), panel.border = structure(list(fill = NA,              colour = "grey50", size = NULL, linetype = NULL), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), panel.grid.major = structure(list(colour = "grey90",              size = 0.2, linetype = NULL, lineend = NULL), .Names = c("colour",          "size", "linetype", "lineend"), class = c("element_line",          "element")), panel.grid.minor = structure(list(colour = "grey98",              size = 0.5, linetype = NULL, lineend = NULL), .Names = c("colour",          "size", "linetype", "lineend"), class = c("element_line",          "element")), panel.margin = structure(0.25, unit = "lines", valid.unit = 3L, class = "unit"),          panel.margin.x = NULL, panel.margin.y = NULL, strip.background = structure(list(             fill = "grey80", colour = "grey50", size = 0.2, linetype = NULL), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), strip.text.x = structure(list(family = NULL,              face = NULL, colour = NULL, size = NULL, hjust = NULL,              vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          strip.text.y = structure(list(family = NULL, face = NULL,              colour = NULL, size = NULL, hjust = NULL, vjust = NULL,              angle = -90, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          plot.background = structure(list(fill = NULL, colour = "white",              size = NULL, linetype = NULL), .Names = c("fill",          "colour", "size", "linetype"), class = c("element_rect",          "element")), plot.title = structure(list(family = NULL,              face = NULL, colour = NULL, size = structure(1.2, class = "rel"),              hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL), .Names = c("family",          "face", "colour", "size", "hjust", "vjust", "angle",          "lineheight"), class = c("element_text", "element")),          plot.margin = structure(c(1, 1, 0.5, 0.5), unit = "lines", valid.unit = 3L, class = "unit")), .Names = c("line",      "rect", "text", "axis.text", "strip.text", "axis.line", "axis.text.x",      "axis.text.y", "axis.ticks", "axis.title.x", "axis.title.y",      "axis.ticks.length", "axis.ticks.margin", "legend.background",      "legend.margin", "legend.key", "legend.key.size", "legend.key.height",      "legend.key.width", "legend.text", "legend.text.align", "legend.title",      "legend.title.align", "legend.position", "legend.direction",      "legend.justification", "legend.box", "panel.background",      "panel.border", "panel.grid.major", "panel.grid.minor", "panel.margin",      "panel.margin.x", "panel.margin.y", "strip.background", "strip.text.x",      "strip.text.y", "plot.background", "plot.title", "plot.margin"     ), class = c("theme", "gg"), complete = TRUE), coordinates = structure(list(         limits = structure(list(x = NULL, y = NULL), .Names = c("x",          "y"))), .Names = "limits", class = c("cartesian", "coord"     )), facet = structure(list(shrink = TRUE), .Names = "shrink", class = c("null",      "facet")), plot_env = , labels = structure(list(         x = "x", y = "y", ymin = "ymin", ymax = "ymax"), .Names = c("x",      "y", "ymin", "ymax"))), .Names = c("data", "layers", "scales",  "mapping", "theme", "coordinates", "facet", "plot_env", "labels" ), class = c("gg", "ggplot"))

值得注意的是,expand参数仅稍微调整图形并expand=c(0,0)不能解决问题.指定限制确实可以解决问题,但也会因某种原因删除轴标签.

任何帮助,将不胜感激.



1> shadow..:

添加时可能已经发生了这种情况g + scale_x_discrete().将离散比例用于连续数据时会发生这种情况.没有休息,您可以看到错误的限制,只需更改它们.

g + scale_x_discrete()
g + scale_x_discrete(limits=1:7)
g + scale_x_discrete(limits=1:7, labels = letters[1:7])

或者,您可以使用factor从一开始就获得适当的限制.当然你必须重命名轴.

ggplot(data=plottingData, aes(x=factor(x), y=y, ymin=ymin, ymax=ymax)) +
  geom_bar(stat="identity", fill=col) +
  geom_errorbar(width=0.5*binwidth, size=0.3) +
  scale_x_discrete(name = 'x')


推荐阅读
  • 本文介绍了Python字典视图对象的示例和用法。通过对示例代码的解释,展示了字典视图对象的基本操作和特点。字典视图对象可以通过迭代或转换为列表来获取字典的键或值。同时,字典视图对象也是动态的,可以反映字典的变化。通过学习字典视图对象的用法,可以更好地理解和处理字典数据。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了PhysioNet网站提供的生理信号处理工具箱WFDB Toolbox for Matlab的安装和使用方法。通过下载并添加到Matlab路径中或直接在Matlab中输入相关内容,即可完成安装。该工具箱提供了一系列函数,可以方便地处理生理信号数据。详细的安装和使用方法可以参考本文内容。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
author-avatar
男人着责任
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有