Question

I have the following means table:

   Sex Trait   Average
1    1    -N  9.042735
2    2    -N  3.529577
3    1     E  8.111111
4    2     E  9.447887
5    1     O 17.196580
6    2     O 16.311800
7    1     A 12.213680
8    2     A 13.449440
9    1     C 12.025640
10   2     C 14.529580

From where I run the following graph:

library(ggplot2)
plot <- ggplot(meansMatrix, aes(Trait, Average, colour= Sex,group= Sex)) +
      geom_line(aes(linetype=Sex),size=1) + 
      geom_point(size=3,fill="white") + 
      scale_color_manual(values = c("black", "grey50")) +
      scale_y_discrete(limits=c(0,18),breaks=seq(2,18,2.5),labels=seq(2,18,2.5)) +
      scale_x_discrete(limits=c("-N","E","O","A","C")); plot

enter image description here There is visible a problem with the y axis. After setting the variable Average as numeric, I have tried with different combinations by changing the arguments (limits, breaks and labels) with no success. This is graph is the only that pops up else than error messages.

Any input of how to re-locate the plot and show the corresponding breaks will be highly appreciated!

Was it helpful?

Solution

Use scale_y_continuous:

meansMatrix <- read.table(text="   Sex Trait   Average
1    1    -N  9.042735
2    2    -N  3.529577
3    1     E  8.111111
4    2     E  9.447887
5    1     O 17.196580
6    2     O 16.311800
7    1     A 12.213680
8    2     A 13.449440
9    1     C 12.025640
10   2     C 14.529580", header=TRUE)

meansMatrix$Sex <- factor(meansMatrix$Sex)


library(ggplot2)
p <- ggplot(meansMatrix, aes(Trait, Average, colour= Sex,group= Sex)) +
  geom_line(aes(linetype=Sex),size=1) + 
  geom_point(size=3,fill="white") + 
  scale_color_manual(values = c("black", "grey50")) +
  scale_y_continuous(limits=c(0,18),breaks=seq(2,18,2.5),labels=seq(2,18,2.5)) +
  scale_x_discrete(limits=c("-N","E","O","A","C")) 

print(p)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top