Tato14
Tato14

Reputation: 435

Error: Aesthetics must be either length 1 or the same as the data (2): x, y

I'm preparing one of the most simple barplots with ggplot2 and I am stuck :(

I want to generate a barplot and I want to add significant asterisk and line but I having problems with the line, here's my code:

p <- ggplot(data=AIG_samples, aes(x=X1, y=Colony_number, fill=X1)) +
  geom_bar(stat="identity", color="black")+
  geom_errorbar(aes(ymin=Colony_number-sd, ymax=Colony_number+sd), width=.1) +
  labs(y="Colony number",x="")+
  theme_minimal()

p + theme(legend.title = element_blank()) + 
  scale_fill_manual(values=c("#999999","#333333"))+
  #Change the square proportion
  coord_fixed(ratio = .008) +
  #Remove X axis labels
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) +
  #Adding asterisks
  geom_path(x=c(1,1,2,2),y=c(480,490,490,480)) +
  annotate("text",x=1.5,y=440,label="*", cex=7)

And this is my data:

X1 | Colony_number | sd

Control|210.5|22.52

sh|387.5|33.96

I know that the error is given by the following line geom_path(x=c(1,1,2,2),y=c(480,490,490,480))

I guess that's kind of stupid issue but I am new in ggplot2 :)

Upvotes: 2

Views: 966

Answers (1)

AK88
AK88

Reputation: 3026

p <- ggplot(data=df, aes(x=X1, y=Colony_number)) +
  geom_bar(aes(fill=X1), stat="identity", color="black")+
  geom_errorbar(aes(ymin=Colony_number-sd, ymax=Colony_number+sd), width=.1) +
  labs(y="Colony number",x="")+
  theme_minimal()

path = data.frame(x=c(1,1,2,2),y=c(480,490,490,480))


p + theme(legend.title = element_blank()) + 
  scale_fill_manual(values=c("#999999","#333333"))+
  #Change the square proportion
  coord_fixed(ratio = .008) +
  #Remove X axis labels
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) +
  #Adding asterisks
  geom_path(data = path, aes(x = x,y = y)) +
  annotate("text",x = 1.5, y = 440, label="*", cex=7)

enter image description here

Upvotes: 1

Related Questions