0

I'm using ggplot2 to plot a data series - performance vs. number of threads, for several different numbers of loop iterations. I've gotten the data points to show up where I need them, and I've used geom_line() to connect the points.

I want to get smooth lines, rather than jagged lines, connecting these points. Not lines of fit / trend lines - just nice-looking splines similar to what you'd get with Excel or Google Sheets when you create a smooth line graph.

I'm pretty sure the best way to do this is to use the spline() function in combination with geom_line(), but I'm stumped on how exactly to do it. At some point I got one spline to show up for the entire graph, but I seem to have broken that. At no point have I been able to get multiple splines in place of the multiple jagged lines shown in the image below.

ggplot seems the shortest path to what I'm trying to achieve, but I'm open to suggestions that don't use it.

Here's what I've got right now in my R script, creating the jagged lines:

#!/usr/bin/Rscript

# Read from stdin (pipe from runProj2 output) and write to txt before mod
data <- read.table('stdin', header=TRUE)
write.table(data, "data.txt", sep="\t", row.names=FALSE)
# Get rid of volume field to analyze performance vs. threads and nodes
data$Volume <- NULL

# Organize data by both threads and nodes & output for PDF incorporation
dataByThreads <- reshape(data, idvar = "Threads", timevar = "Nodes", direction = "wide")
dataByNodes   <- reshape(data, idvar = "Nodes", timevar = "Threads", direction = "wide")

write.table(dataByThreads, "dataByThreads.txt", sep="\t", row.names=FALSE)
write.table(dataByNodes, "dataByNodes.txt", sep="\t", row.names=FALSE)

# Plot
library(ggplot2)
library(reshape2)

# Melt data back into long format for ggplot
threads_long <- melt(dataByThreads,  id="Threads")
nodes_long   <- melt(dataByNodes, id="Nodes")

ggplot(data=threads_long,
       aes(x=Threads, y=value, color=variable)) +
       geom_point() +
       geom_line() +
       labs(y='MegaHeights per Second', x='Threads', color='Nodes') +
       ggtitle('Performance vs. Threads')

ggplot(data=nodes_long,
       aes(x=Nodes, y=value, color=variable)) +
       geom_point() +
       geom_line() +
       labs(y='MegaHeights per Second', x='Nodes', color='Threads') +
       ggtitle('Performance vs. Threads')

Here's what I've got so far:

from link

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
D. LaMartina
  • 3
  • 1
  • 2
  • 1
    You might be looking for `geom_xspline()` from package **ggalt**. See an example in [this answer](https://stackoverflow.com/questions/53896973/emulating-google-sheets-smoothed-line-chart-with-ggplot2/53897075#53897075). – aosmith Apr 17 '19 at 13:35
  • That worked - thanks! Bit of a pain to get ggalt working, given some other non-R dependencies, but the end product looks great. – D. LaMartina Apr 17 '19 at 23:55

0 Answers0