[R] Some help understanding ggplot2

Glenn Schultz glennmschultz at me.com
Sat Nov 28 21:34:47 CET 2015


All,

I am trying to format a graph using scales and percent format but I am missing something and all my graphics books on ggplot2 are now quite far behind where ggplot2 is today.    I seem to be missing a trick but the documentation implies that percent_format() will multiply by 100 and add %.  I think the problem has to do with normalizing the data.

Glenn


Here is my data
structure(list(Rate = c(0.004975, 0.0126625, 0.0180375, 0.0136, 
0.016, 0.0196, 0.023, 0.0256, 0.0293, 0.0324, 0.0338), Maturity = c(0.0833, 
0.25, 0.5, 1, 2, 3, 4, 5, 7, 10, 30)), .Names = c("Rate", "Maturity"
), row.names = c("ED1M", "ED3M", "ED6M", "USSW1", "USSW2", "USSW3", 
"USSW4", "USSW5", "USSW7", "USSW10", "USSW30"), class = "data.frame")

data <- data.frame(Rates[1:2,2:12])
data <- data.frame(t(data))
Note: if I do not normalize the data by dividing by 100 percent_format() works but then 2.25% is 225%
data[,1] <- data[,1]/100
colnames(data) <- c("Rate", "Maturity")


Here is the color palette
cbbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", 
"#000000","#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#000000")


This code does not work and returns an error
ggplot(data, aes(x=Maturity, y = Rate, colour = 'Rate')) +
geom_line(size = 1.5) +
theme_minimal()+
theme(panel.grid.major = element_line(size = .25, color = "grey")) +
scale_y_continuous(breaks = c(seq(0, 0.05, .0025)), percent_format()) +
scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 7, 10, 30)) +
ylab("Swap Rate (%)") +
xlab("Maturity") +
theme(axis.text.y = element_text(size = 15)) +
theme(axis.text.x = element_text(angle = 0, size = 15)) +
theme(axis.title = element_text(size = 20)) +
scale_colour_manual(values = cbbPalette, guide = FALSE)

This code works but no percent
ggplot(data, aes(x=Maturity, y = Rate, colour = 'Rate')) +
geom_line(size = 1.5) +
theme_minimal()+
theme(panel.grid.major = element_line(size = .25, color = "grey")) +
scale_y_continuous(breaks = c(seq(0, 0.05, .0025))) +
scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 7, 10, 30)) +
ylab("Swap Rate (%)") +
xlab("Maturity") +
theme(axis.text.y = element_text(size = 15)) +
theme(axis.text.x = element_text(angle = 0, size = 15)) +
theme(axis.title = element_text(size = 20)) +
scale_colour_manual(values = cbbPalette, guide = FALSE)


More information about the R-help mailing list