0

I'm trying plots a graph lines using ggplot library in R, but I get a good plots but I need reduce the gradual space or height between rows grid lines because I get big separation between lines.

This is my R script:

library(ggplot2)
library(reshape2)

data <- read.csv('/Users/keepo/Desktop/G.Con/Int18/input-int18.csv')

chart_data <- melt(data, id='NRO')
names(chart_data) <- c('NRO', 'leyenda', 'DTF')

ggplot() +
  geom_line(data = chart_data, aes(x = NRO, y = DTF, color = leyenda), size = 1)+
  xlab("iteraciones") +
  ylab("valores")

and this is my actual graphs: enter image description here

..the first line is very distant from the second. How I can reduce heigth?

regards.

GMs
  • 47
  • 5
  • If your data is that far apart, that's an accurate representation of the data. – Jack Brookes Mar 31 '18 at 22:56
  • You could use `+ facet_grid(leyenda~., scales = 'free')` to separate the three lines. – Jack Brookes Mar 31 '18 at 22:58
  • Thanks for yours answers. @Jack B., I know, it is obviously, but I need the reduction. I trying your piece of scripts, but this doesn't work for me because I must be a simple convergency and that way not is recommendable for my work. – GMs Mar 31 '18 at 23:07

2 Answers2

1

The lines are far apart because the values of the variable plotted on the y-axis are far apart. If you need them closer together, you fundamentally have 3 options:

  1. change the scale (e.g. convert the plot to a log scale), although this can make it harder for people to interpret the numbers. This can also change the behavior of each line, not just change the space between the lines. I'm guessing this isn't what you will want, ultimately.
  2. normalize the data. If the actual value of the variable on the y-axis isn't important, just standardize the data (separately for each value of leyenda).
  3. As stated above, you can graph each line separately. The main drawback here is that you need 3 graphs where 1 might do.

Not recommended:

  1. I know that some graphs will have the a "squiggle" to change scales or skip space. Generally, this is considered poor practice (and I doubt it's an option in ggplot2 because it masks the true separation between the data points. If you really do want a gap, I would look at this post: axis.break and ggplot2 or gap.plot? plot may be too complexe

In a nutshell, the answer here depends on what your numbers mean. What is the story you are trying to tell? Is the important feature of your plots the change between them (in which case, normalizing might be your best option), or the actual numbers themselves (in which case, the space is relevant).

Melissa Key
  • 4,476
  • 12
  • 21
  • Hello @Melissa, I need to compare the three lines only visually. Each line represent an efficiency of algorithm represented by values. – GMs Mar 31 '18 at 23:18
  • What are you comparing? If it's the absolute score, then the plot is accurate as is. If you want the relative change as a function of your x-variable, then normalize your scores. Perhaps you want both - in which case, use both plots. – Melissa Key Mar 31 '18 at 23:23
  • The 3 lines are the result of algorithms that learn (heuristic), each algorithm had to minimize a quality value, then each line says how was the learning of each algorithm as long as it minimizes. The graph is just to look at the behavior, just that. – GMs Mar 31 '18 at 23:27
0

you could use an axis transformation that maps your data to the screen in a non-linear fashion,

fun_trans <- function(x){

  d <- data.frame(x=c(800, 2500, 3100), y=c(800,1950, 3100))
  model1 <- lm(y~poly(x,2), data=d)
  model2 <- lm(x~poly(y,2), data=d)

  scales::trans_new("fun", 
                    function(x) as.vector(predict(model1,data.frame(x=x))), 
                    function(x) as.vector(predict(model2,data.frame(y=x))))
}

last_plot() + scale_y_continuous(trans = "fun")

enter image description here