![]() Again, it is also important that the font change is optional and it’s only to obtain a more similar result compared to the original. P4 <- ggplot () + theme_economist () + scale_fill_economist () + theme ( plot.title = element_text ( family = "OfficinaSanITC-Book" ), text = element_text ( family = "OfficinaSanITC-Book" )) + geom_bar ( aes ( y = percentage, x = year, fill = product ), data = charts.data, stat = "identity" ) + geom_text ( data = charts.data, aes ( x = year, y = pos, label = paste0 ( percentage, "%" )), colour = "white", family = "OfficinaSanITC-Book", size = 4 ) + theme ( legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank ()) + scale_x_continuous ( breaks = seq ( 2006, 2014, 1 )) + scale_y_continuous ( labels = dollar_format ( suffix = "%", prefix = "" )) + labs ( x = "Year", y = "Percentage" ) + ggtitle ( "Composition of Exports to China (%)" ) p4īelow we’ve applied theme_fivethirtyeight(), which approximates graphs in the nice FiveThirtyEight website. For an exact result you need ‘Officina Sans’ which is a commercial font and is available here. It is also important that the font change argument inside theme is optional and it’s only to obtain a more similar result compared to the original. ![]() Below we’ve applied theme_economist(), which approximates graphs in the Economist magazine. There are a wider range of pre-built themes available as part of the ggthemes package (more information on these here). #font_import(pattern="kcd") #fonts() fill <- c ( "#56B4E9", "#F0E442" ) p4 <- ggplot () + geom_bar ( aes ( y = percentage, x = year, fill = product ), data = charts.data, stat = "identity" ) + geom_text ( data = charts.data, aes ( x = year, y = pos, label = paste0 ( percentage, "%" )), colour = "black", family = "xkcd-Regular", size = 5, show.legend = F ) + theme ( legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank ()) + scale_x_continuous ( breaks = seq ( 2006, 2014, 1 )) + scale_y_continuous ( labels = dollar_format ( suffix = "%", prefix = "" )) + labs ( x = "Year", y = "Percentage" ) + ggtitle ( "Composition of Exports to China (%)" ) + scale_fill_manual ( values = fill ) + theme ( axis.line = element_line ( size = 1, colour = "black" ), = element_blank (), = element_blank (), panel.border = element_blank (), panel.background = element_blank ()) + theme ( plot.title = element_text ( family = "xkcd-Regular" ), text = element_text ( family = "xkcd-Regular" ), = element_text ( colour = "black", size = 10 ), = element_text ( colour = "black", size = 10 )) p4 In order to create this chart, you first need to import the XKCD font, install it on your machine and load it into R using the extrafont package. Below is an example of a theme Mauricio was able to create which mimics the visual style of XKCD. ggplot2 allows for a very high degree of customisation, including allowing you to use imported fonts. ![]() Of course, you may want to create your own themes as well. P4 <- ggplot () + theme_bw () + geom_bar ( aes ( y = percentage, x = year, fill = product ), data = charts.data, stat = "identity" ) + geom_text ( data = charts.data, aes ( x = year, y = pos, label = paste0 ( percentage, "%" )), size = 4 ) + theme ( legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank ()) + scale_x_continuous ( breaks = seq ( 2006, 2014, 1 )) + scale_y_continuous ( labels = dollar_format ( suffix = "%", prefix = "" )) + labs ( x = "Year", y = "Percentage" ) + ggtitle ( "Composition of Exports to China (%)" ) p4
0 Comments
Leave a Reply. |