Coder Social home page Coder Social logo

friday-trick's Introduction

Gggplot tricks

Bar plot width

Why the bar widths were so fat ?????? when the levels of categorical variable were missing.

Let’s fix it.

# read data
adsl <- read_xpt("data/adsl.xpt")
color_fill <- c("#008080","#35aab2","#83d4da")
# create count table
dt <- adsl  %>% count(ARM,RACE) %>% group_by(ARM) %>% mutate( per = (n/ sum(n))*100) %>% mutate( count = paste0(n, " (", sprintf('%.1f', per), "%)"))
source("source/trick_20220506.R")

The bar width is too big???? What happened?

print(dt)
## # A tibble: 7 × 5
## # Groups:   ARM [3]
##   ARM                  RACE                                 n   per count     
##   <chr>                <chr>                            <int> <dbl> <chr>     
## 1 Placebo              BLACK OR AFRICAN AMERICAN            8  9.30 8 (9.3%)  
## 2 Placebo              WHITE                               78 90.7  78 (90.7%)
## 3 Xanomeline High Dose AMERICAN INDIAN OR ALASKA NATIVE     1  1.19 1 (1.2%)  
## 4 Xanomeline High Dose BLACK OR AFRICAN AMERICAN            9 10.7  9 (10.7%) 
## 5 Xanomeline High Dose WHITE                               74 88.1  74 (88.1%)
## 6 Xanomeline Low Dose  BLACK OR AFRICAN AMERICAN            6  7.14 6 (7.1%)  
## 7 Xanomeline Low Dose  WHITE                               78 92.9  78 (92.9%)
p1 <- ggplot(dt, aes(y = n, x = RACE, colour = ARM, fill = ARM)) +
  geom_bar(stat = 'identity',position = position_dodge(0.95), width = 0.9) +
  geom_text(aes(label = count, colour = ARM), size = 4,vjust= -0.5, position = position_dodge(0.95)) +
  scale_color_manual("",values = c(color_fill)) + 
  scale_fill_manual('', values = c(color_fill))
p1 <- gg_plot_theme(p1, y_label = "Count", x_label = "Race", title = "Why the first bar width is so BIG?", legend = "none")

plot

Let fix it! I need to add preserve = “single” to position = position_dodge(0.95,preserve = “single”)

Guess what?

p2 <- ggplot(dt, aes(y = n, x = RACE, colour = ARM, fill = ARM)) +
  geom_bar(stat = 'identity',, width = 0.9) +
  geom_text(aes(label = count, colour = ARM), size = 4,vjust= -0.5, position = position_dodge(0.95)) +
  scale_color_manual("",values = c(color_fill)) + 
  scale_fill_manual('', values = c(color_fill))
p2 <- gg_plot_theme(p2, y_label = "Count", x_label = "Race", title = "Adding preserve = 'single'", legend = "none")

plot

Yeah, it fixed……but……why the labels were off. Oh man, google ……

I get it, let use complete() to fix the data

# Fill data with NA
dt_fill <- data.frame(dt)
dt_fill  <- complete(dt_fill,ARM,RACE)
p3 <- ggplot(dt_fill, aes(y = n, x = RACE, colour = ARM, fill = ARM)) +
     geom_bar(stat = 'identity',position = position_dodge(0.95,preserve = "single"), width = 0.9) +
     geom_text(aes(label = count, colour = ARM), size = 4,vjust= -0.5, position = position_dodge(0.95)) +
     scale_color_manual("",values = c(color_fill)) + 
     scale_fill_manual('', values = c(color_fill))
p3 <- gg_plot_theme(p3, y_label = "Count", x_label = "Race", title = " Fill data with complete()", legend = "none")

Finally I got what I wanted

plot

friday-trick's People

Contributors

loankimrobinson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.