JC3019
JC3019

Reputation: 383

Manually changing line size in ggplot

Still getting to grips with ggplot. My question: How do I manually change the line size? I've tried with scale_size_manual but it didn't seem to work.

setup:

test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")

ggplot(test.mat, aes(x=Window, y=value)) +
  geom_line(aes(colour = type, linetype = type)) +
  theme_classic() +
  scale_colour_manual("type", values = c("black", "steelblue")) +
  scale_linetype_manual("type", values = c("solid", "solid")) +
  scale_size_manual("type", values = c(5, 1.4), guide = "none")

Upvotes: 0

Views: 3002

Answers (3)

D. Bontempo
D. Bontempo

Reputation: 186

To follow up on my comment on deepseefan's answer

base + 
  geom_line(aes(colour = type, 
                size=factor(ifelse(type=="Cohort Alpha", "thick",  "thin"),
                            levels=c("thick","thin")))) +
  scale_colour_manual(values = c("black", "steelblue")) +
  scale_size_manual(values = c(5, 1.4), guide = FALSE)

Upvotes: 0

deepseefan
deepseefan

Reputation: 3791

Just turning @NelsonGon comment into an answer.

Is this what you want?

test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
# -------------------------------------------------------------------------
base <- ggplot(test.mat, aes(x=Window, y=value)) 

#Here is where you need to add size  
line_size <- base + geom_line(aes(colour = type, linetype = type), size=3)
line_size + theme_classic() +
  scale_colour_manual("type", values = c("black", "steelblue")) +
  scale_linetype_manual("type", values = c("solid", "solid")) +
  scale_size_manual("type", values = c(5, 1.4), guide = "none")

output

output_nelson

Update

If you want variable thickness for the individual lines, you can do as follows.

base <- ggplot(test.mat, aes(x=Window, y=value)) 
#Use an ifelse to add variable thickness 
line_size <- base + geom_line(aes(colour = type, size=ifelse(type=="Cohort Alpha",1,2)))

line_size + guides(size = FALSE) 

var_thickness

Upvotes: 0

JC3019
JC3019

Reputation: 383

specify size inside aes() function as follows:

ggplot(test.mat, aes(x=Window, y=value)) +
  geom_line(aes(colour = type, linetype = type, size = type)) +
  theme_classic() +
  scale_colour_manual("type", values = c("black", "steelblue")) +
  scale_linetype_manual("type", values = c("solid", "solid")) +
  scale_size_manual("type", values = c(5, 1.4), guide = "none")

Upvotes: 3

Related Questions