Use of scale_x_discrete in R ggplot2

前端 未结 2 1484
醉梦人生
醉梦人生 2021-02-14 15:05

I have a problem using the discrete scale in ggplot2 in R. The use of

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

incorrectly changes in limit

相关标签:
2条回答
  • 2021-02-14 15:45

    p1 + scale_x_continuous(breaks = c(2, 4, 6))

    0 讨论(0)
  • 2021-02-14 15:57

    That probably already happens when you add g + scale_x_discrete(). This happens when using a discrete scale for continuous data. Without the breaks, you can see the wrong limits and just change them.

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

    Alternatively, you can use factor to get the proper limits from the beginning. Of course you'll have to rename the axis.

    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')
    
    0 讨论(0)
提交回复
热议问题