Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8305863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:12:47+00:00 2026-06-08T18:12:47+00:00

I am new to ggplot so bear with me. I am charting out growth

  • 0

I am new to ggplot so bear with me. I am charting out growth projections for 35 small-area geographies which is an unhealthy amount for one plot even with use of the fantastic directlabels library. However I need all the series for initial screening.

The challenge is to make it readable. I found a fix by @Ben Bolker for using large numbers of distinct colors but am having trouble varying the linetype. The 35 series don’t need to be unique, but I would like to use the 12 different types to make individual series easier to read.

My plan was to create a random list with 35 elements of the 12 possible types and pass that as the linetype argument, but I am having trouble getting it to work, with the error:

Error: Aesthetics must either be length one, or the same length as the dataProblems:lty

I have 35 values in the linetype list. Of course I would like for the types, colors and all to be reflected in the legend.

The melted data looks like this; 9 years’ observations for each of 35 series:

> simulation_long_index[16:24,]    
      year    geography    value
16    2018    sfr_2    101.1871
17    2019    sfr_2    101.1678
18    2020    sfr_2    101.2044
19    2012    sfr_3    100.0000
20    2013    sfr_3    100.1038
21    2014    sfr_3    100.2561
22    2015    sfr_3    100.0631
23    2016    sfr_3    100.8071
24    2017    sfr_3    101.2405    

Here is my code so far:

lty <- data.frame(lty=letters[1:12][sample(1:12, 35,replace=T)])

g3<-ggplot(data=simulation_long_index,
   aes(
     x=as.factor(year), 
     y=value, 
     colour=geography,
     group=geography,
     linetype=lty$lty))+
       geom_line(size=.65) + 
       scale_colour_manual(values=manyColors(35)) +
     geom_point(size=2.5) +
     opts(title="growth")+
     xlab("Year") + 
     ylab(paste("Indexed Value (Rel. to 2012")) + 
     opts(axis.text.x=theme_text(angle=90, hjust=0))

print(g3)

adding

    scale_linetype_manual("",values=lty$lty) +

after scale_color_manual instead of the linetype argument produces the chart, but lines are all the same. How, then, do I get the lines to vary for large series counts?

example plot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:12:48+00:00Added an answer on June 8, 2026 at 6:12 pm

    The trick with using scale_..._manual is often to send a named vector as the value argument. The setNames function is good for this

    First, some dummy data

    ## some dummy data 
    simulations<- expand.grid(year = 2012:2020, geography = paste0('a',1:35))
    library(plyr)
    library(RColorBrewer)
    simulation_long_index <- ddply(simulations, .(geography), mutate, 
      value = (year-2012) * runif(1,-2, 2) + rnorm(9, mean = 0, sd = runif(1, 1, 3)))
    ## create a manyColors function 
    manyColors <- colorRampPalette(brewer.pal(name = 'Set3',n=11))
    

    Next we create a vector that is a random sample from 1:12 (with replacement) and set the names the same as the geography variable

    lty <- setNames(sample(1:12,35,T), levels(simulation_long_index$geography))
    

    This is what it looks like

    lty
    ## a1  a2  a3  a4  a5  a6  a7  a8  a9 a10 a11 a12 a13 a14 a15 a16 
    ## 7   5   8  11   2  10   3   2   5   4   6   6  11   8   2   2 
    ## a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 
    ## 12   7   6   8  11   5   1   1   8  12   8   1  12   2   3   5 
    ## a33 a34 a35 
    #7   1   3 
    

    Now you can use line_type = geography in conjunction with scale_linetype_manual(values = lty)

    ggplot(data=simulation_long_index,
            aes(
              x=as.factor(year), 
              y=value, 
              colour=geography,
              group=geography,
              linetype = geography))+
                geom_line(size=.65) + 
                scale_colour_manual(values=manyColors(35)) +
                geom_point(size=2.5) +
                opts(title="growth")+
                xlab("Year") + 
                ylab(paste("Indexed Value (Rel. to 2012")) + 
                opts(axis.text.x=theme_text(angle=90, hjust=0)) +
                scale_linetype_manual(values = lty)
    

    Which gives you

    enter image description here

    As an aside, do you really want to plot the years as a factor variable?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to ggplot and I have some questions. I hope someone can
I am relatively new to ggplot, so please forgive me if some of my
I'm fairly new to ggplot. I have made a bumpplot using code posted below.
I am new in R and am trying to so some graphics using ggplot
I am new to Sweave/Tex and would like to keep my R code out
New to WPF so I'm sure it's an easy one this; for every control
as a new ggplot2 user, I am a bit lost with the amount of
New Android programmer here. I have a Service which performs socket management and async
New to Python and trying to figure out what went wrong here. Making a
new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println(run); throw new SomeRandomException(); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.