|
| 1 | +library(plotly) |
| 2 | +set_credentials_file("ggplot2-cookbook", "gzcn4660jr") |
| 3 | +py <- plotly(); |
| 4 | + |
| 5 | +library(ggplot2) |
| 6 | + |
| 7 | +bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() |
| 8 | +bp |
| 9 | + |
| 10 | +bp + coord_flip() |
| 11 | + |
| 12 | +# Manually set the order of a discrete-valued axis |
| 13 | +bp + scale_x_discrete(limits=c("trt1","trt2","ctrl")) |
| 14 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/discrete valued axes')) |
| 15 | + |
| 16 | +# Reverse the order of a discrete-valued axis |
| 17 | +# Get the levels of the factor |
| 18 | +flevels <- levels(PlantGrowth$group) |
| 19 | +# "ctrl" "trt1" "trt2" |
| 20 | +# Reverse the order |
| 21 | +flevels <- rev(flevels) |
| 22 | +# "trt2" "trt1" "ctrl" |
| 23 | +bp + scale_x_discrete(limits=flevels) |
| 24 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/reversed ordered axes - 1')) |
| 25 | + |
| 26 | +# Or it can be done in one line: |
| 27 | +bp + scale_x_discrete(limits = rev(levels(PlantGrowth$group)) ) |
| 28 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/reversed ordered axes - 2')) |
| 29 | + |
| 30 | +bp + scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"), labels=c("Control", "Treat 1", "Treat 2")) |
| 31 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/setting tick mark labels')) |
| 32 | + |
| 33 | +# Hide x tick marks, labels, and grid lines |
| 34 | +bp + scale_x_discrete(breaks=NULL) |
| 35 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/hidden tick marks, labels, gridline')) |
| 36 | + |
| 37 | +# Hide all tick marks and labels (on X axis), but keep the gridlines |
| 38 | +bp + theme(axis.ticks = element_blank(), axis.text.x = element_blank()) |
| 39 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/hidden tick marks and labels')) |
| 40 | + |
| 41 | +# Set the range of a continuous-valued axis |
| 42 | +# These are equivalent |
| 43 | +bp + ylim(0,8) |
| 44 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/set range of continuous-valued axis - 1')) |
| 45 | +bp + scale_y_continuous(limits=c(0,8)) |
| 46 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/set range of continuous-valued axis - 2')) |
| 47 | + |
| 48 | +# These two do the same thing; all data points outside the graphing range are dropped, |
| 49 | +# resulting in a misleading box plot |
| 50 | +bp + ylim(5, 7.5) |
| 51 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/misleading range')) |
| 52 | +bp + scale_y_continuous(limits=c(5, 7.5)) |
| 53 | + |
| 54 | +# Using coord_cartesian "zooms" into the area |
| 55 | +bp + coord_cartesian(ylim=c(5, 7.5)) |
| 56 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/coord_cartesian')) |
| 57 | + |
| 58 | +# Specify tick marks directly |
| 59 | +bp + coord_cartesian(ylim=c(5, 7.5)) + |
| 60 | + scale_y_continuous(breaks=seq(0, 10, 0.25)) # Ticks from 0-10, every .25 |
| 61 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/specify tick marks directly')) |
| 62 | + |
| 63 | +# Reverse order of a continuous-valued axis |
| 64 | +bp + scale_y_reverse() |
| 65 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/reverse y scale')) |
| 66 | + |
| 67 | +# Setting the tick marks on an axis |
| 68 | +# This will show tick marks on every 0.25 from 1 to 10 |
| 69 | +# The scale will show only the ones that are within range (3.50-6.25 in this case) |
| 70 | +bp + scale_y_continuous(breaks=seq(1,10,1/4)) |
| 71 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/manual tick marks')) |
| 72 | + |
| 73 | +# The breaks can be spaced unevenly |
| 74 | +bp + scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6,8)) |
| 75 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/uneven tick marks')) |
| 76 | + |
| 77 | +# Suppress ticks and gridlines |
| 78 | +bp + scale_y_continuous(breaks=NULL) |
| 79 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/suppress y ticks, labels, and gridlines')) |
| 80 | + |
| 81 | +# Hide tick marks and labels (on Y axis), but keep the gridlines |
| 82 | +bp + theme(axis.ticks = element_blank(), axis.text.y = element_blank()) |
| 83 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/suppress y ticks and labels')) |
| 84 | + |
| 85 | +# Create some noisy exponentially-distributed data |
| 86 | +set.seed(201) |
| 87 | +n <- 100 |
| 88 | +dat <- data.frame(xval = (1:n+rnorm(n,sd=5))/20, yval = 2*2^((1:n+rnorm(n,sd=5))/20)) |
| 89 | + |
| 90 | +# A scatterplot with regular (linear) axis scaling |
| 91 | +sp <- ggplot(dat, aes(xval, yval)) + geom_point() |
| 92 | +py$ggplotly(sp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/linear axes')) |
| 93 | +sp |
| 94 | + |
| 95 | +# log2 scaling of the y axis (with visually-equal spacing) |
| 96 | +library(scales) # Need the scales package |
| 97 | +sp + scale_y_continuous(trans=log2_trans()) |
| 98 | +py$ggplotly(sp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/ln y axes with visual-equal spacing')) |
| 99 | + |
| 100 | +# log2 coordinate transformation (with visually-diminishing spacing) |
| 101 | +sp + coord_trans(y="log2") |
| 102 | +py$ggplotly(sp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/ln y axes with visually diminishing spacing')) |
| 103 | + |
| 104 | +sp + scale_y_continuous(trans = log2_trans(), |
| 105 | + breaks = trans_breaks("log2", function(x) 2^x), |
| 106 | + labels = trans_format("log2", math_format(2^.x))) |
| 107 | +py$ggplotly(sp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/ln y axes with exponent tick marks')) |
| 108 | + |
| 109 | +set.seed(205) |
| 110 | +n <- 100 |
| 111 | +dat10 <- data.frame(xval = (1:n+rnorm(n,sd=5))/20, yval = 10*10^((1:n+rnorm(n,sd=5))/20)) |
| 112 | + |
| 113 | +sp10 <- ggplot(dat10, aes(xval, yval)) + geom_point() |
| 114 | + |
| 115 | +# log10 |
| 116 | +sp10 + scale_y_log10() |
| 117 | +py$ggplotly(sp10, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/log_10 y axes')) |
| 118 | + |
| 119 | +# log10 with exponents on tick labels |
| 120 | +sp10 + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), |
| 121 | + labels = trans_format("log10", math_format(10^.x))) |
| 122 | +py$ggplotly(sp10, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/log_10 y axes with exponent tick marks')) |
| 123 | + |
| 124 | +# Data where x ranges from 0-10, y ranges from 0-30 |
| 125 | +set.seed(202) |
| 126 | +dat <- data.frame(xval = runif(40,0,10), yval = runif(40,0,30)) |
| 127 | +sp <- ggplot(dat, aes(xval, yval)) + geom_point() |
| 128 | + |
| 129 | +# Force equal scaling |
| 130 | +sp + coord_fixed() |
| 131 | +py$ggplotly(sp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/forced equal spacing')) |
| 132 | + |
| 133 | +# Equal scaling, with each 1 on the x axis the same length as y on x axis |
| 134 | +sp + coord_fixed(ratio=1/3) |
| 135 | +py$ggplotly(sp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/forced equal scaling')) |
| 136 | + |
| 137 | +bp + theme(axis.title.x = element_blank()) + # Remove x-axis label |
| 138 | + ylab("Weight (Kg)") # Set y-axis label |
| 139 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/axes labels')) |
| 140 | + |
| 141 | +# Also possible to set the axis label with the scale |
| 142 | +# Note that vertical space is still reserved for x's label |
| 143 | +bp + scale_x_discrete(name="") + |
| 144 | + scale_y_continuous(name="Weight (Kg)") |
| 145 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/axes labels set with scale')) |
| 146 | + |
| 147 | +# Change font options: |
| 148 | +# X-axis label: bold, red, and 20 points |
| 149 | +# X-axis tick marks: rotate 90 degrees CCW, move to the left a bit (using vjust, |
| 150 | +# since the labels are rotated), and 16 points |
| 151 | +bp + theme(axis.title.x = element_text(face="bold", colour="#990000", size=20), |
| 152 | + axis.text.x = element_text(angle=90, vjust=0.5, size=16)) |
| 153 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/axes labels with formatting')) |
| 154 | + |
| 155 | +# Label formatters |
| 156 | +library(scales) # Need the scales package |
| 157 | +bp + scale_y_continuous(labels=percent) + |
| 158 | + scale_x_discrete(labels=abbreviate) # In this particular case, it has no effect |
| 159 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/axes labels with percent labels')) |
| 160 | + |
| 161 | +# Self-defined formatting function for times. |
| 162 | +timeHMS_formatter <- function(x) { |
| 163 | + h <- floor(x/60) |
| 164 | + m <- floor(x %% 60) |
| 165 | + s <- round(60*(x %% 1)) # Round to nearest second |
| 166 | + lab <- sprintf('%02d:%02d:%02d', h, m, s) # Format the strings as HH:MM:SS |
| 167 | + lab <- gsub('^00:', '', lab) # Remove leading 00: if present |
| 168 | + lab <- gsub('^0', '', lab) # Remove leading 0 if present |
| 169 | +} |
| 170 | + |
| 171 | +bp + scale_y_continuous(label=timeHMS_formatter) |
| 172 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/axes labels with custom time labels')) |
| 173 | + |
| 174 | +# Hide all the gridlines |
| 175 | +bp + theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) |
| 176 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/hidden gridlines')) |
| 177 | + |
| 178 | +# Hide just the minor gridlines |
| 179 | +bp + theme(panel.grid.minor=element_blank()) |
| 180 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/hidden minor gridlines')) |
| 181 | + |
| 182 | +# Hide all the horizontal gridlines |
| 183 | +bp + theme(panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank()) |
| 184 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/hidden horizontal gridlines')) |
| 185 | + |
| 186 | +# Hide all the vertical gridlines |
| 187 | +bp + theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) |
| 188 | +py$ggplotly(bp, kwargs=list(fileopt='overwrite', filename='R-Cookbook/axes/hidden vertical gridlines')) |
0 commit comments