CaitlinG
CaitlinG

Reputation: 2015

Setting the labels on the x-axis of a graph

I am attempting to graph the following data:

 ph1 = c(5, 6, 7, 8, 9)
 ph2 = ph3 = ph1

e1 = c(0.191, 0.154, 0.179, 0.073, 0.009)
e2 = c(0, 0.029, 0.054, 0.055, 0.024)
e3 = c(0.019, 0.027, 0.063, 0.029, 0.039)

set.seed(1)
df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
                   e2 = sort(runif(5, 0.05, 0.25)),
                   e3 = sort(runif(5, 0.05, 0.25)),
                   ph1 = sort(runif(5, 1, 100)),
                   ph2 = sort(runif(5, 1, 100)),
                   ph3 = sort(runif(5, 1, 100))
                   )
### reshape this to give a column indicating group
 df2 <- with(df1,
         as.data.frame(cbind( c(ph1, ph2, ph3),
                             c(e1, e2, e3),
                             rep(seq(3), each=5) )
                       ))
 colnames(df2) <- c("ph","maltose_in_mg","team")
 df2$team <- as.factor(df2$team)
 library(ggplot2)
 ggplot(df2, aes(x=ph, y=maltose_in_mg, col=team)) + geom_line()

...with the pH values (5 through 9) on the x-axis as labels. Unfortunately, the labels are being displayed from 0 to 100.

Edit (Note: Non-functional solution):

df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
                  e2 = sort(runif(5, 0.05, 0.25)),
                  e3 = sort(runif(5, 0.05, 0.25)),
                  ph1 = sort(runif(1, 5, 9)),
                  ph2 = sort(runif(1, 5, 9)),
                  ph3 = sort(runif(1, 5, 9))
                  )

Upvotes: 2

Views: 555

Answers (1)

Statwonk
Statwonk

Reputation: 724

I think this is what you're trying to do, but if it's not, please correct me.

I've put pH on the x-axis, and Maltose in Milligrams on the y-axis.

I also added a theme from the ggthemes package, which makes it pretty. There are other themes you can find at the link I put in the code comment.

Finally, I limited the x-axis with coord_cartesian (<-- that's what I think you were looking for, control over the range that's displayed) and then scale_colour_discrete to put a nice looking title on the legend.

Next I added a title with ggtitle and axis labels. The theme_wsj() has ggplot settings which you can view just by typing theme_wsj() into the R console. It's default is to hide axis labels, I had to overide that with the theme() function and the bits inside of it.

ggplot2 is an amazing package. You can read all about it here: http://docs.ggplot2.org/current/

install.packages("ggthemes")
library(ggthemes)

ph1 = c(5, 6, 7, 8, 9)
ph2 = ph3 = ph1

e1 = c(0.191, 0.154, 0.179, 0.073, 0.009)
e2 = c(0, 0.029, 0.054, 0.055, 0.024)
e3 = c(0.019, 0.027, 0.063, 0.029, 0.039)

set.seed(1)
df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
                  e2 = sort(runif(5, 0.05, 0.25)),
                  e3 = sort(runif(5, 0.05, 0.25)),
                  ph1 = sort(runif(1, 5, 9)),
                  ph2 = sort(runif(1, 5, 9)),
                  ph3 = sort(runif(1, 5, 9))
)

df2 <- with(df1,
            as.data.frame(cbind( c(ph1, ph2, ph3),
                                 c(e1, e2, e3),
                                 rep(seq(3), each=5) )
            ))
colnames(df2) <- c("ph", "maltose_in_mg", "team")
df2$team <- as.factor(df2$team)
library(ggplot2)
ggplot(df2, aes(x = ph, y = maltose_in_mg, colour = team)) + 
  geom_line(size = 2) +
  coord_cartesian(xlim = c(5, 9)) + # this is how you limit the axis range displayed, you can do the y, too with ylim = c(0, 1)
  scale_colour_discrete(name = "Team") + 
  theme_wsj() + # find more themes here: https://github.com/jrnold/ggthemes
  ggtitle("pH by Team") +
  ylab("Maltose in Milligrams") +
  xlab("pH") +
  theme(axis.title = element_text(angle = 90, vjust = -0.075),
        axis.text = element_text(size = 20),
        legend.text = element_text(size = 15))

Upvotes: 2

Related Questions