USING DIFFERENT THEMES IN R FOR DATA VISUALIZATION





There are several built-in themes available in R. Some common themes include:
👉theme_grey() applies a simple grey background to the plot.
👉theme_bw() removes colors and grid lines from the plot.
👉 theme_minimal() and theme_light() provide a more modern and clean look to the plot. 
👉theme_classic() has a white background, grid lines, and a black axis line. It also includes a title, axis labels, and a legend, if applicable. This theme is often used for traditional data visualizations and is a good choice for plots that will be printed or included in reports.
👉theme_dark() is often used for data visualizations that will be displayed on a screen, such as in a dashboard or web application. The dark background helps to make the data points and other visual elements stand out, making the plot easier to read.
👉theme_linedraw() is a function in the R programming language that is used to apply the "linedraw" theme to a plot or graph. This theme is based on the "linedraw" visual style, which is characterized by simple, clean lines and a minimalistic color scheme.
👉theme_void() is a function in the R programming language that is used to remove all non-data elements from a plot or graph. This includes axes, titles, labels, and other visual elements that are not directly related to the data being plotted. The resulting plot will consist only of the data and any geometric objects used to represent it, such as lines, points, or bars.

Users can also create their own custom themes using the theme() function and specifying the desired visual formatting options.

In this example, the custom theme sets the background color of the plot to dark gray, and increases the font size of the plot title to 18 points. You can modify other elements of the plot in the same way, such as the axis labels, tick marks, and legend. You can also use the theme() function to combine multiple themes, either by creating a new theme that combines elements from different existing themes, or by applying multiple themes to the same plot.

library(ggplot2)

Create some sample data
x = rnorm(50)
y = rnorm(50)

Use ggplot() to create a basic scatter plot
ggplot(data = data.frame(x, y), aes(x = x, y = y)) +
  geom_point()


Create a custom theme using the theme() function
my_theme = theme(plot.background = element_rect(fill = "darkgray"),
                  plot.title = element_text(size = 18))

Apply the custom theme to the plot
ggplot(data = data.frame(x, y), aes(x = x, y = y)) +
  geom_point() +
  my_theme



I will use the birthwt dataset for this blog post. The birthwt dataset is a popular dataset used in statistics and machine learning research. It contains information on the birth weights of babies born in the United States. The data is often used to study the relationship between a baby's birth weight and various factors such as the mother's age, weight, and smoking habits. By analyzing the data, researchers can gain insights into the factors that influence a baby's birth weight and develop strategies to improve birth outcomes.

install.packages(MASS, dependencies = TRUE )
library(MASS)

Feature Descriptions
low : indicator of birth weight less than 2.5 kg.
age : mother's age in years.
lwt : mother's weight in pounds at last menstrual period.
race : mother's race (1 = white, 2 = black, 3 = other).
smoke : smoking status during pregnancy.
ptl : number of previous premature labours.
ht : history of hypertension.
ui : presence of uterine irritability.
ftv : number of physician visits during the first trimester.
bwt : birth weight in grams.

>str(birthwt)
'data.frame': 189 obs. of  10 variables:
 $ low  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ age  : int  19 33 20 21 18 21 22 17 29 26 ...
 $ lwt  : int  182 155 105 108 107 124 118 103 123 113 ...
 $ race : int  2 3 1 1 1 3 1 3 1 1 ...
 $ smoke: int  0 0 1 1 1 0 0 0 1 1 ...
 $ ptl  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ ht   : int  0 0 0 0 0 0 0 0 0 0 ...
 $ ui   : int  1 0 0 1 1 0 0 0 0 0 ...
 $ ftv  : int  0 3 1 2 0 0 1 1 1 0 ...
 $ bwt  : int  2523 2551 2557 2594 2600 2622 2637 2637 2663 2665 ...

I will make some examples about the theme below
👉I used the ggplot2 package in R to create a scatterplot of birth weight (bwt) versus mother's age (age), with the points colored according to whether the mother smoked during pregnancy (smoke). The geom_jitter() function adds random "jitter" to the points to avoid overplotting, while geom_abline() adds a grey, dashed line to the plot. The theme_economist() function from the ggthemes package applies an Economist-style theme to the plot, and the labs() function is used to add labels for the x and y axes and a title to the plot. Finally, the scale_color_brewer() function is used to apply a sequential color scheme from the RColorBrewer package to the plotted points.
ggplot(birthwt, aes(age, bwt, color = factor(smoke))) +
  geom_jitter() +
  geom_abline(colour = "grey50", linewidth = 2) +
  ggthemes::theme_economist()+
  labs(x = "Mother's Age in Years",
       y  = "Birth Weight in Grams",
       title = "AGE-BWT-SMOKE") +
  scale_color_brewer(type = "seq", palette = "Spectral") 


theme_bw()
ggplot(birthwt, aes(age, bwt, color = factor(smoke))) +
  geom_jitter() +
  geom_abline(colour = "grey50", linewidth = 2) +
  labs(x = "Mother's Age in Years",
       y  = "Birth Weight in Grams",
       title = "AGE-BWT-SMOKE") +
  scale_color_brewer(type = "seq", palette = "Spectral") +  
  theme_bw()


theme() 
👉I used the ggplot2 package in R to create a scatterplot of the number of previous premature labors (ptl) versus the number of physician visits during the current pregnancy (ftv), with the points colored according to the mother's race.

👉 The theme() function is used to bold and increase the size of the plot title, change the appearance of the legend, adjust the position of the legend, and customize the appearance of the tick marks and grid lines. For example, the plot.title element is set to use bold, 12-point text, while the legend.background element is set to use a white fill with a white border and a line width of 4.

ggplot(birthwt, aes(ftv, ptl, color = factor(race))) +
  geom_jitter() +
  geom_abline(colour = "grey50", linewidth = 2) +
  labs(x = "number of physician visits",
       y  = "number of previous premature labours",
       title = "FTV-PTL-RACE") +
  scale_color_brewer(type = "seq", palette = "Spectral") + 
  theme(plot.title = element_text(face = "bold", size = 12),
        legend.background = element_rect(fill = "white",
                                         linewidth = 4,
                                         colour = "white"),
        legend.justification = c(0,1),
        legend.position = c(0,1),
        axis.ticks = element_line(colour = "grey70", linewidth = 0.2),
        panel.grid.major = element_line(colour = "grey70", linewidth = 0.2),
        panel.grid.minor = element_blank()
  )

theme_dark()
ggplot(birthwt) +
  geom_point(aes(x = age, y = lwt, colour = factor(smoke))) +
  labs(
    title = "AGE-LWT-SMOKE",
    subtitle = "USA",
    caption = "It contains information on the birth weights of babies born in the United States.",
    tag = "Figure 1",
    x = "mother's age in years",
    y = "mother's weight",
    colour = "smoke") +
  theme_dark()

hrbrthemes
👉ggplot2 does not provide many built-in options for modifying the appearance of a plot, so users often have to write their own custom themes to change the appearance of their plots.
👉The hrbrthemes package offers a convenient solution to this problem by providing a collection of pre-made ggplot2 themes and scales that can be easily applied to a plot. These themes and scales can be used to quickly and easily change the appearance of a ggplot2 plot, without having to write custom code. This can save time and effort for users who want to create visually appealing plots with ggplot2.
install.packages("hrbrthemes")
library(hrbrthemes)
ggplot(birthwt, aes(x = age, fill = race)) +
  geom_density(alpha = 0.7) + 
  ggtitle("Plot title") +
  theme_ipsum() + # Arial Narrow
  scale_fill_ipsum() +
  theme(legend.position = "top")


theme_calc()
👉The ggthemes package is a collection of pre-made themes and scales for ggplot2 that can be easily applied to a plot to change its appearance. This theme is based on the look and feel of the Calc spreadsheet program, and it is designed to provide a clean, professional look to a plot.

ggplot(data = birthwt,
       mapping = aes(x=age)) + 
  geom_boxplot(outlier.color="red",
               notch=FALSE,
               fill="blue") +
  coord_flip() +
  facet_grid(. ~smoke) +
  ggthemes::theme_calc()



You can follow me on Linkedin and github.

https://www.linkedin.com/in/adem-bak%C4%B1rc%C4%B1/

https://github.com/edmbkrc



Comments

Popular posts from this blog