Control column widths in a ggplot2 graph with a series and inconsistent data

后端 未结 2 877
一向
一向 2020-12-04 00:01

In the artificial data I have created for the MWE below I have tried to demonstrate the essence of a script I have created in R. As can be seen by the graph that gets produ

相关标签:
2条回答
  • 2020-12-04 00:37

    At the expense of doing your own calculation for the x coordinates of the bars as shown below, you can get a chart which may be close to what you're looking for.

    x <- c("1","2","3","1","2","3","4")
    s <- c("No","No","No","Yes","Yes","Yes","Yes")
    y <- c(1,2,3,2,3,4,5)
    df <- data.frame(cbind(x,s,y) )
    df$x_pos[order(df$x, df$s)] <- 1:nrow(df)
    x_stats <- as.data.frame.table(table(df$x), responseName="x_counts")
    x_stats$center <- tapply(df$x_pos, df$x, mean)
    df <-  merge(df, x_stats, by.x="x", by.y="Var1", all=TRUE)
    bar_width <- .7
    df$pos <- apply(df, 1, function(x) {xpos=as.numeric(x[4]) 
                                    if(x[5] == 1) xpos 
                                    else ifelse(x[2]=="No", xpos + .5 -        bar_width/2, xpos - .5 + bar_width/2) } )
     print(df)
    gg <- ggplot(data=df, aes(x=pos, y=y, fill=s ) )
    gg <- gg + geom_bar(position="identity", stat="identity", size=.3,    colour="black", width=bar_width)
    gg <- gg + scale_x_continuous(breaks=df$center,labels=df$x )
    plot(gg)
    

    ----- edit --------------------------------------------------

    Modified to place the labels at the center of bars.

    Gives the following chart

    enter image description here

    0 讨论(0)
  • 2020-12-04 01:02

    Yeah, I figured what happened: you need to be extra careful about factors being factors and numerics being numerics. In my case, with stringsAsFactors = FALSE I have

    str(df)
    'data.frame':   7 obs. of  3 variables:
     $ x: chr  "1" "2" "3" "1" ...
     $ s: chr  "No" "No" "No" "Yes" ...
     $ y: chr  "1" "2" "3" "2" ...
    
    dput(df)
    structure(list(x = c("1", "2", "3", "1", "2", "3", "4"), s = c("No", 
    "No", "No", "Yes", "Yes", "Yes", "Yes"), y = c("1", "2", "3", 
    "2", "3", "4", "5")), .Names = c("x", "s", "y"), row.names = c(NA, 
    -7L), class = "data.frame")
    

    with no factors and numeric turned into character because of cbind-ing (sic!). Let us have another data frame:

    dff <- data.frame(x = factor(df$x), s = factor(df$s), y = as.numeric(df$y))
    

    Adding a "dummy" row (manually for your example, check out expand.grid version in the linked question on how to do this automatically):

    dff <- rbind(dff, c(4, "No", NA))
    

    Plotting (I removed extra aes):

    ggplot(data = df3, aes(x, y, fill=s)) + 
      geom_bar(position=dodge_str, stat="identity", size=.3, colour="black")
    

    enter image description here

    0 讨论(0)
提交回复
热议问题