6

this is my script and the associated plot:

    library(ggplot2)
    library(reshape)
    df <- structure(list(ID = structure(1:19, .Label = c("2818/22/0834", 
   "2818/22/0851", "2818/22/0853", "2818/22/0886", "B0F", "B12T", 
    "B1T", "B21T", "B22F", "B26T", "B33F", "B4F", "P1", "P21", "P24", 
    "P25", "P27", "P28", "P29"), class = "factor"), K = c(0.089, 
    0.094, 0.096, 0.274, 0.09, 0.312, 0.33, 0.178, 0.05, 0.154, 0.083, 
    0.098, 0.035, 0.084, 0.053, 0.061, 0.043, 0.094, 0.101), Na = c(2.606, 
    3.822, 4.977, 2.522, 15.835, 83.108, 52.041, 41.448, 11.849, 
    40.531, 5.854, 10.151, 3.52, 8.445, 5.273, 7.246, 6.177, 14.813, 
    15.569), Cl = c(3.546, 6.181, 8.422, 3.733, 14.685, 96.911, 65.518, 
    79.01, 10.349, 53.361, 6.12, 10.832, 2.313, 10.312, 5.641, 8.708, 
    6.138, 12.302, 20.078), Mg = c(1.487, 1.773, 1.992, 1.143, 2.991, 
    1.678, 2.23, 3.288, 1.148, 2.428, 3.428, 2.729, 0.777, 2.554, 
    2.374, 4.075, 1.993, 1.881, 3.034), Ca = c(5.529, 6.205, 6.59, 
    4.099, 10.631, 4.564, 6.652, 13.374, 4.332, 10.542, 11.194, 10.053, 
    2.969, 7.73, 8.163, 11.539, 6.166, 5.968, 9.299), SO4 = c(0.663, 
    0.831, 0.607, 0.882, 9.013, 0.896, 0.652, 0.021, 1.446, 0.012, 
    8.832, 6.665, 1.003, 2.575, 3.685, 7.121, 3.64, 5.648, 2.397), 
        HCO3 = c(7.522, 5.498, 6.15, 5.242, 8.582, 4.067, 5.65, 9.364, 
        5.435, 8.068, 9.054, 8.326, 4.805, 7.235, 7.488, 9.234, 6.352, 
        6.98, 8.34)), .Names = c("ID", "K", "Na", "Cl", "Mg", "Ca", 
    "SO4", "HCO3"), class = "data.frame", row.names = c(NA, -19L))

    df_melted<-melt(df, na.rm=T)

    ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
      geom_point(size=2) +
      geom_line() +
      theme(legend.position="none") +
      scale_y_log10(breaks=seq(0, 100, 10))

Is there a way to remove the spaces at the beginning and at the end of the plot? I tried with xlim but the problem is that the x variable is not a numerical variable, so, something like xlim(c("K", "HCO3")) doesn't work. enter image description here

1 Answer 1

7

This is a discrete scale, but you can still used the expand argument as follows. Whether the output looks acceptable or not is another matter. Play with the c(0,0) values until you find something that suits. Using 0.1 for the second value gives a slightly better plot, in my view...

ggplot(df_melted, aes(variable, value, group=ID, color=ID))+
      geom_point(size=2) +
      geom_line() +
      theme(legend.position="none") +
      scale_y_log10(breaks=seq(0, 100, 10)) +
        scale_x_discrete(expand = c(0,0))

screenshot

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. For who is asking like myself how to do the same with the y-axis, I used the obvious scale_y_discrete(expand = c(0,0)) and it worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.