How to Increase the Line Width For One Group in ggplot

For many graph makers, optimizing your visual requires alterations to the preprogrammed interface. For programmers utilizing ggplot, the question of how to alter the line width to emphasize a particular data trend has been hotly discussed on many platforms. Many replies match the numerous methods to reach the same goal, so here we’ll look at the best use for most common cases in line graph plotting.

Let’s bring up a common issue of plotting where one line needs to be thicker than the rest in a representation of multiple lines. We’ll set the line widths manually by assigning each class and providing an axis label to differentiate the line value. The geom line function will connect the data values with the visual lines, mapping out our data set.

ggplot(aes(time, value, color = class)) +
geom_line(aes(size = class)) +
scale_size_manual(values = c(a = thick, b = thin, c = thin))

This manual method is usually the simplest to implement, but other programmers have wanted tools to further customize their data visuals. It’s worth noting that other in other instances you may wish to simply split the data set and represent each class with its own graph, but know that you will have to specify the parameters of each line making for a lot of coding work.
data.frame(x=c(1, 2, 3, 4, 5, 6, 7),
y=c(6, 8, 12, 14, 11, 10, 15))

plotA ggplot(df, aes(x=x,y=y)) + geom_line() + ggtitle(“Size = 1 (Default)”)
plotB ggplot(df, aes(x=x,y=y)) + geom_line(size=1.5) + ggtitle(“Size = 1.5”)
plotC ggplot(df, aes(x=x,y=y)) + geom_line(size=2) + ggtitle(“Size = 2”)
plotD ggplot(df, aes(x=x,y=y)) + geom_line(size=3) + ggtitle(“Size = 3”)

grid.arrange(plot1, plot2, plot3, plot4, ncol=1)

The highlight tool best emphasizes outlying values from a dense plot of lines. Implemented correctly, the line type will be highlighted if its value meets the set criteria in the highlights parameters.

ggplot(d) +
geom_line(aes(idx, value, colour = type)) +
gghighlight(max(value) > 21, mean(flag) > 5)

In this command line, the lines that get highlighted represent values falling between the maximum and minimum parameters to filter through the many lines.

Obviously from what we’ve seen this tool has high functionality, but there is also numerous tools that accompany the ggplot in making it more visually appealing.

gplot(df) + geom_line(aes(x = x, y = year, group = year, size = values))
print(p)

gplot(df) + geom_line(aes(x = x, y = year, group = year, colour = year, size = values))
print(p)

gplot(df) +
geom_line(aes(x = x, y = values, group = amount, colour = amount, size = value)) +
geom_smooth(aes(x = x, y = values, group = amount, size = values), method = “loess”) + facet_wrap(~amount)

The plot and geom line tool are all that’s really necessary for the graph, but in optimizing the customizable format of your line graph geom_smooth gives greater depth of alteration by specifying the color and size for each value. As your library of tools increases, more tools will allow for greater changes and more specific alterations to your dataset.

Scroll to top
Privacy Policy