Question: How to plot variables in ggplot2 based on color by value
1
gravatar for mohammedtoufiq91
10 months ago by
mohammedtoufiq91110 wrote:

Hi,

I have a question about plotting variables based on the color by value using the ggplot2 library. There are 3 columns in the data frame (Var1, Var2, and value). Below is the code I am running in R. I keep getting the error message Aesthetics must be either length 1 or the same as the data (27): fill. Please let me know how to resolve this.

Thank you,

Toufiq

Input

mod.group_test <- read.csv(file ="./mod.group_test.csv", stringsAsFactors = F)

dput(mod.group_test)
structure(list(Var1 = structure(c(1L, 6L, 13L, 23L, 24L, 25L, 
26L, 27L, 2L, 3L, 4L, 5L, 7L, 8L, 9L, 10L, 11L, 12L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L), .Label = c("A1", "A15", "A16", 
"A17", "A18", "A2", "A24", "A25", "A26", "A27", "A28", "A29", 
"A3", "A30", "A31", "A32", "A33", "A34", "A35", "A36", "A37", 
"A38", "A4", "A5", "A6", "A7", "A8"), class = "factor"), Var2 = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), value = c(-0.584795322, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -8.461538462, 0, 0, 
0, 0, 0, 0.5859375, 0, 0.732600733, -4.545454545, 0, 0)), row.names = c(NA, 
-27L), class = "data.frame")

class(mod.group_test)
[1] "data.frame"

mod.group_test$Var1 <- as.factor(mod.group_test$Var1)

str(mod.group_test)
'data.frame':   27 obs. of  3 variables:
 $ Var1 : Factor w/ 27 levels "A1","A15","A16",..: 1 6 13 23 24 25 26 27 2 3 ...
 $ Var2 : int  1 1 1 1 1 1 1 1 1 1 ...
 $ value: num  -0.585 0 0 0 0 ...

dev.new()
png(filename = paste0("Test.png"), res = 600, height = 5.5, width = 8.5, units = "in")
ggplot(mod.group_test, aes(Var1, as.factor(Var2))) +
  geom_tile(color="#E6E6E6" , size = 0.2, fill= color)+
  geom_point(aes(colour=value,size=1))+ 
  theme(axis.text.x = element_text(angle = -90, hjust = 0))+
  labs(title= "Test")+
  ylab("") +
  xlab("") +
  scale_color_gradient2(low = "blue", mid="white", high = "red", limits=c(-100,100),na.value = "#E6E6E6", guide = "colourbar")+
  theme_light() +
  theme(panel.grid.minor = element_line(colour="black", size=0.9))+
  coord_flip() + scale_x_discrete(limits = rev(levels(mod.group_test$Var1))) +
  theme(panel.border = element_rect(color = "black",size = 0.5),
        axis.text.x = element_text(colour="black",size=9,angle=0,hjust=0.5,vjust=2,face="plain"),
        axis.text.y = element_text(colour="black",size=9,angle=0,hjust=0.5,vjust=0.5,face="plain"))

dev.off()


Error: Aesthetics must be either length 1 or the same as the data (27): fill

Expected output: I would like to plot something similar to this based on the values

ADD COMMENTlink modified 10 months ago • written 10 months ago by mohammedtoufiq91110
2

Most of the data values are zero (median =0) and data has extreme values due to which gradient doesn't work proper. There is unnecessary coordinating flipping and tile option (IMO) in this plot. Gradient range is also incorrect (c(-100,100)). Please start with simplest ggplot2 code and add decorators later on.

Try this with dput data frame:

ggplot(df, aes( as.factor(Var2),Var1)) +
    geom_point(aes(color=value, size=value))+
    scale_color_gradient2(low = "blue", mid = "white",high = "red", midpoint = mean(df$value))
ADD REPLYlink modified 10 months ago • written 10 months ago by cpad011214k

@cpad0112,

Excellent. Thank you very much. However, I observe that on the y-axis the values are ordered in the "Descending order" (for instance, from top to bottom -A8, A7, A6, ................A1). Is there a way I can arrange (A1, A2, A3, A4..........A38)

ADD REPLYlink written 10 months ago by mohammedtoufiq91110
1

try this:

library(ggplot2)
library(naturalsort)
ggplot(df, aes(Var2,naturalfactor(Var1)))+
    geom_point(aes(size=value))
ADD REPLYlink written 10 months ago by cpad011214k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 917 users visited in the last hour