Analysis in R: Focus on a specific group! The “ggfocus” package

This is an introduction to the “ggplot2” package, which allows you to easily create graphs that focus on specific groups. For example, you can create a scatterplot that shows a particular group in color and other groups in black and white. It is a useful package.

Package version is 1.0.0. Checked with R version 4.2.2.

Install Package

Run the following command.

#Install Package
install.packages("ggfocus")

Example

See the command and package help for details.

#Loading the library
library("ggfocus")

###Create Data#####
#Install the tidyverse package if it is not already there
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}
set.seed(1234)
n <- 50
TestData <- tibble(Group = sample(paste0("Group", 1:4), n,
                                  replace = TRUE),
                   X_num_Data = sample(c(1:50), n, replace = TRUE),
                   Y_num_Data = sample(c(51:100), n, replace = TRUE))

#Create base ggplot
#Scatter plot
Point_Plot <- ggplot(TestData, aes(x = X_num_Data, y = Y_num_Data)) +
  geom_point()
#Bar chart
Column_Plot <- ggplot(TestData, aes(x = X_num_Data, y = Y_num_Data,
                                    fill = Group)) +
  geom_col()
#Line plot
Line_Plot <- ggplot(TestData, aes(x = 1:50, y = Y_num_Data,
                                  group = Group, linetype = Group)) +
  geom_line() 
########

#Attention in scatter plot: scale_color_focus command
#Specify target: focus_levels option; specified by string
#Specify color: color_focus option
Point_Plot + aes(colour = Group, size = 5) + 
  scale_color_focus(focus_levels = c("Group2", "Group4"),
                    color_focus = c("red", "blue")) +
  guides(size = "none")

#Attention in bar chart: scale_fill_focus command
#Specify target: focus_levels option; specified by string
#Specify color: color_focus
Column_Plot +
  scale_fill_focus(focus_levels = c("Group2", "Group4"),
                   color_focus = c("red", "blue"))

#Attention in line plot: scale_linetype_focus command
#Point to specify group, linetype for aes in the model
#Specify target:focus_levels option; specified by string
#Specify target linetype: linetype_focus option
#Specify other linetypes: linetype_other option
Line_Plot +
  scale_linetype_focus(focus_levels = c("Group2", "Group4"),
                       linetype_focus = c(5, 1),
                       linetype_other = 3)

#Focus on symbol type and size: scale_shape_focus command &
#scale_size_focus command
##scale_shape_focus command
#Specify target symbols:shape_focus option
#Specify symbols not in focus: shape_other option
Point_Plot + aes(size = Group, shape = Group) +
  scale_shape_focus(focus_levels = c("Group2", "Group4"),
                    shape_focus = c(18, 21), shape_other = 1) +
##scale_size_focus command
#Specify target symbol size: size_focus option
#Specify symbol size outside of target: size_other option
  scale_size_focus(focus_levels = c("Group2", "Group4"),
                   size_focus = 7, size_other = 3)

Output Example

・scale_color_focus command

・scale_fill_focus command

・scale_linetype_focus command

・scale_shape_focus command & scale_size_focus command


I hope this makes your analysis a little easier !!

価格および発送可能時期は、変更される場合があります。購入時には、商品詳細ページに表示されている価格および発送可能時期が適用されます。
本サイト上に表示されるコンテンツは、Amazonによって提供されています。このコンテンツは、現状のまま提供され、変更または削除される場合があります。
Amazonのアソシエイトとして、からだにいいものは適格販売により収入を得ています。
Copied title and URL