How to create ridge line and wordcloud plot in R rogramming

 

Basic ridgeline plot

 Ridgelineplot (formerly called Joyplot) allows to study the distribution of a numeric variable for several groups. In this example, we check the distribution of diamond prices according to their quality.

This graph is made using the ggridges library, which is a ggplot2 extension and thus respect the syntax of the grammar of graphic. We specify the price column for the X axis and the cut column for the Y axis. Adding fill=cut allows to use one colour per category and display them as separate groups.

# library

library(ggridges)

library(ggplot2)

 # Diamonds dataset is provided by R natively

#head(diamonds)

 # basic example

ggplot(diamonds, aes(x = price, y = cut, fill = cut)) +

  geom_density_ridges() +

  theme_ridges() +

  theme(legend.position = "none")

 

Shape Variation

It is possible to represent the density with different aspects. For instance, using stat="binline" makes a histogram like shape to represent each distribution.

# library

library(ggridges)

library(ggplot2)

library(dplyr)

library(tidyr)

library(forcats)

 # Load dataset from github

data <- read.table("https://raw.githubusercontent.com/zonination/perceptions/master/probly.csv", header=TRUE, sep=",")

data <- data %>%

  gather(key="text", value="value") %>%

  mutate(text = gsub("\\.", " ",text)) %>%

  mutate(value = round(as.numeric(value),0)) %>%

  filter(text %in% c("Almost Certainly","Very Good Chance","We Believe","Likely","About Even", "Little Chance", "Chances Are Slight", "Almost No Chance"))

 # Plot

data %>%

  mutate(text = fct_reorder(text, value)) %>%

  ggplot( aes(y=text, x=value,  fill=text)) +

    geom_density_ridges(alpha=0.6, stat="binline", bins=20) +

    theme_ridges() +

    theme(

      legend.position="none",

      panel.spacing = unit(0.1, "lines"),

      strip.text.x = element_text(size = 8)

    ) +

    xlab("") +

    ylab("Assigned Probability (%)")

 

Color relative to numeric value

It is possible to set color depending on the numeric variable instead of the categorical one.

# library

library(ggridges)

library(ggplot2)

library(viridis)

library(hrbrthemes)

 # Plot

ggplot(lincoln_weather, aes(x = `Mean Temperature [F]`, y = `Month`, fill = ..x..)) +

  geom_density_ridges_gradient(scale = 3, rel_min_height = 0.01) +

  scale_fill_viridis(name = "Temp. [F]", option = "C") +

  labs(title = 'Temperatures in Lincoln NE in 2016') +

  theme_ipsum() +

    theme(

      legend.position="none",

      panel.spacing = unit(0.1, "lines"),

      strip.text.x = element_text(size = 8)

    )

 

Wordcloud

wordcloud (or tag cloud) is a visual representation of text data. Tags are usually single words, and the importance of each tag is shown with font size or color. In R, two packages allow to create wordclouds: Wordcloud and Wordcloud2.

Most basic with wordcloud2()

This is the most basic barplot you can build with the wordcloud2 library, using its wordcloud2() function. Note:

  • data is a data frame including word and freq in each column
  • size is the font size, default is 1.

# library

library(wordcloud2)

 # have a look to the example dataset

# head(demoFreq)

# Basic plot

wordcloud2(data=demoFreq, size=1.6)

 

Color & Background color

It is possible to change the word color using the color argument. You can provide a vector of color, or use random-dark or random-light. You can also customize the color with backgroundColor.

# library

library(wordcloud2)

 # Gives a proposed palette

wordcloud2(demoFreq, size=1.6, color='random-dark')

 # or a vector of colors. vector must be same length than input data

wordcloud2(demoFreq, size=1.6, color=rep_len( c("green","blue"), nrow(demoFreq) ) )

 # Change the background color

wordcloud2(demoFreq, size=1.6, color='random-light', backgroundColor="black")

 

Shape

ou can custom the wordcloud shape using the shape argument. Available shapes are:

·         circle

·         cardioid

·         diamond

·         triangle-forward

·         triangle

·         pentagon

·         star

You can also use an image like this one as a mask.

# library

library(wordcloud2)

 

# Change the shape:

wordcloud2(demoFreq, size = 0.7, shape = 'star')

 # Change the shape using your image

wordcloud2(demoFreq, figPath = "~/Desktop/R-graph-gallery/img/other/peaceAndLove.jpg", size = 1.5, color = "skyblue", backgroundColor="black")

 

Word orientation

Rotate words with 3 arguments: minRotation, maxRotation and rotateRatio.

# library

library(wordcloud2)

 # wordcloud

wordcloud2(demoFreq, size = 2.3, minRotation = -pi/6, maxRotation = -pi/6, rotateRatio = 1)

 

Chinese version

# library

library(wordcloud2)

 # wordcloud

wordcloud2(demoFreqC, size = 2, fontFamily = "????????????", color = "random-light", backgroundColor = "grey")

 

Use letter or text as shape

The letterCloud function allows to use a letter or a word as a mask for the wordcloud:

# library

library(wordcloud2)

 letterCloud( demoFreq, word = "R", color="random-light" , backgroundColor="black")

letterCloud( demoFreq, word = "PEACE", color="white", backgroundColor="pink")

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction