2

I have a dataframe which has two different sample types (A and B). I would like to differentiate these by using different shape options. Here is a dataframe and my current attempt at performing this.

output of dput(head(df))

structure(list(Mean.Count = c(30404.8407153174, 15689.4221807262, 30404.8407153174, 15689.4221807262), 
              Log2FC = c(-0.00357013689574257, -0.00417251481039714, 0.306809506669248, 0.224653107007472), 
               Adj.P.Value = c(0.988865360408676, 0.981816989495127, 0.00202882891738576, 
 2.72576774009609e-05), 
                TimeKD = c("A", "A", "B", "B"), 
                Gene = c("HSPA5","MYH9", "HSPA5", "MYH9")), 
                row.names = c("HSPA5", "MYH9", "HSPA51", "MYH91"),
                 class = "data.frame")

Current attempt

ggplot(df, aes(x = Gene, y = Log2FC, group=TimeKD)) + 
  geom_point(aes(color = -Adj.P.Value, size = Mean.Count), alpha = 0.5)+
  coord_flip() +
  scale_colour_gradientn(
    colours = grDevices::colorRampPalette(c("black", "cyan", "violet"))(n = 200),
    values = NULL,
    space = "Lab",
    na.value = "grey50",
    guide = "colourbar",
    aesthetics = "colour"
  )

enter image description here

Currently both A and B samples are circles. Can I use ggplot2 to change one of them into another shape?

Any help would be appreciated.

Krutik
  • 461
  • 4
  • 13

1 Answers1

2

You can add shape = TimeKD to the aes of the geom_point call like this...

ggplot(df, aes(x = Gene, y = Log2FC, group=TimeKD)) + 
  geom_point(aes(color = -Adj.P.Value, 
                 size = Mean.Count, 
                 shape = TimeKD), # <-- Right here! 
             alpha = 0.5)+
  coord_flip() +
  scale_colour_gradientn(
    colours = grDevices::colorRampPalette(c("black", "cyan", "violet"))(n = 200),
    values = NULL,
    space = "Lab",
    na.value = "grey50",
    guide = "colourbar",
    aesthetics = "colour"
  )

Which would look like this...

the requested graph with different shapes for the groups

Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122