R script, plotting a plot with categorical data, high light some points with different colors using ggplot2

huang 0 like s 380 view s

Tags:

    # load packages
    library(tidyverse)
    library(palmerpenguins)
    library(ggbeeswarm)
    library(ggforce)

    #remotes::install_github("allisonhorst/palmerpenguins")
    # peek at penguins data
    #glimpse(penguins)

    # create some example data
    x <- c(rep("A", 100), rep("B", 100), rep("C", 100))
    y <- rnorm(300)

    # create a data frame with x, y, and color columns
    df <- data.frame(x = x, y = y, color = ifelse(c(1:300) %in% c(5, 10, 15), "Highlighted", "Normal"))

    #  geom_point(size = 3) +
    # plot the data with points colored by category and highlight
    ggplot(df, aes(x = x, y = y, color = color)) +
      scale_color_manual(values = c("Normal" = "black", "Highlighted" = "red")) +
      geom_beeswarm(cex = 1.5) +
      theme_classic()

    #In this script, we use ggplot2 to create a scatter plot with categorical data and highlight some points. We start by creating an example dataset with a categorical variable x and a continuous variable y. We then create a data frame df with x, y, and color columns. The color column is set to "Highlighted" for the points we want to highlight, and "Normal" for the rest.

    #We then use ggplot2 to plot the data. We set x, y, and color to the corresponding columns in df using the aes() function. We use geom_point() to plot the points, and set the size argument to control the size of the points. We use scale_color_manual() to set the colors for the "Normal" and "Highlighted" categories. Finally, we use theme_classic() to set the theme of the plot to a classic theme.

like unlike

点赞本文的读者

还没有人对此文章表态


本文有评论

没有评论

看文章,发评论,不要沉默


© 2023 XGenes.com Impressum