0

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")
4
  • 2
    Maybe use size in geom_line? Most functions have a size argument anyways. Unless you have some other size mapping in mind, using the size arg should suffice. Commented Sep 28, 2019 at 11:47
  • @NelsonGon I have tried size = c(5,1.4)) but it doesn't work. Any suggestions Commented Sep 28, 2019 at 12:01
  • 2
    Your scale_size_manual is having no effect because you do not have a size aesthetic in your ggplot. Add size=type inside the aes(...) argument. Commented Sep 28, 2019 at 12:06
  • 1
    Try this: iris %>% ggplot(aes(Sepal.Length, size=Species, y=Petal.Length,color=Species))+ geom_point()+ geom_line()+ scale_size_manual(values=c(1,2,17)) Commented Sep 28, 2019 at 12:06

3 Answers 3

3

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")
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I want any arbitrary difference in the line sizes. These look the same size to me?
This is creating a mapping for size aesthetic that is numeric, so a range of increasing sizes from 1 to 2 (likely counting by .2) is used, and only the thinnest and thickest are used. A discrete mapping could be made with ifelse(type=="Cohort Alpha","thin","thick) - BUT thin will only be thinner if it is the 1st observed level. You could use factor(ifelse(type=="Cohort Alpha","thin","thick"), levels=c("thick","thin") - BUT to specify the actual sizes you also need + scale_size_manual(values = c(1.8, .75)). Or other thick and thin size choices.
0

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)

Comments

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.