简体   繁体   中英

R - ggplot2 change in sorting in geom_bar (?)

Yesterday I have produced some old charts and was surprised that sorting in geom_bar - bar and pie charts has changed.

Not so long ago I posted this question

R - Strange pie chart behavior in ggplot

In the beginning of it I presented an example - the same short code produces now a different chart. Did I misunderstand something? This is rather surprising for me.

Thank you for any comments.

The code:

library(ggplot2)
library(data.table)

c1 <- c(2,3)
c2 <- c("second","third")
c2 <- factor(c2, levels = c("first","second","third","fourth"))
c3 <- c(0.7,0.3)
cs <- data.frame(c1,c2,c3)
ct <- data.table(cs)
colx <- c("blue","red")
midpoint <- cumsum(ct$c3) - ct$c3/2

ct

keycols = c("c1")
setkeyv(ct,keycols)


vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) + 
          geom_bar(stat="identity",width=2) + 
          coord_polar(theta='y')+
          theme(axis.ticks=element_blank(), axis.title=element_blank(), 
            axis.text.y = element_blank(), panel.grid  = element_blank(),
            axis.text.x = element_text(color=colx,size=15,hjust=0))+ 
        scale_y_continuous(breaks = midpoint, labels = ct$c2)  + 
        scale_fill_manual(values=colx) +
        scale_x_continuous(limits=c(-1,2.5))
vysg

It produces this chart

在此处输入图片说明

whereas the original chart (produced by exactly the same code looked like this)

在此处输入图片说明

I have noticed end of order statement but it should not affect this example.

Thank you for any comments, I might be just blind to something evident.

I have asked on Github as prompted by Axeman.

The reason is the change of order in stacking bar charts.

http://ggplot2.tidyverse.org/articles/releases/ggplot2-2.2.0.html

To keep the same results as earlier it is necessary to cope with this situation. Several possibilities are prompted in the link. In my situation I have changed the direction in the coord_polar and transformed the midpoint

breaks = 1-midpoint #instead of: midpoint
coord_polar(theta='y', direction=-1)  #instead of: coord_polar(theta='y')

The whole script:

library(ggplot2)
library(data.table)

c1 <- c(2,3)
c2 <- c("second","third")
c2 <- factor(c2, levels = c("first","second","third","fourth"))
c3 <- c(0.7,0.3)
cs <- data.frame(c1,c2,c3)
ct <- data.table(cs)
colx <- c("blue","red")
midpoint <- cumsum(ct$c3) - ct$c3/2

ct

keycols = c("c1")
setkeyv(ct,keycols)


vysg <- ggplot(ct, aes(x=1,y=c3,fill=c2)) + 
          geom_bar(stat="identity",width=2) + 
          coord_polar(theta='y', direction=-1)+
          theme(axis.ticks=element_blank(), axis.title=element_blank(), 
            axis.text.y = element_blank(), panel.grid  = element_blank(),
            axis.text.x = element_text(color=colx,size=15,hjust=0))+ 
        scale_y_continuous(breaks = 1-midpoint, labels = ct$c2)  + 
        scale_fill_manual(values=colx) +
        scale_x_continuous(limits=c(-1,2.5))
vysg

now provides the same result in ggplot 2.2.0 as the original script in ggplot 1.0.1

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM