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 8234545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:30:59+00:00 2026-06-07T18:30:59+00:00

I’m using ggplot2 with the directlabels package in a geom_line() plot, and I would

  • 0

I’m using ggplot2 with the directlabels package in a geom_line() plot, and I would like one of the labels to read "X-M". However, in my data.frame() "X-M" as column name gets renamed to "X.M", and I couldn’t find documentation on how to provide the direct.label function with custom label names, nor reading the source helped. (directabels doesn’t seem to honor the label names set in the ggplot scale, which is the first thing that I tried.)

Sample code:

library("scales")
library("reshape2")
library("ggplot2")
library("directlabels")

data = data.frame(
  C = c(1.2, 1.4, 0.3, -2.0, 0.5),
  I = c(1.2, 1.5, -1.3, -3.8, 1.8),
  G = c(0.2, 0.3, 0.3, 0.2, 0.2),
  "X-M" = c(2.9, -0.7, 0.3, -2.8, 1.5) +
          c(-2.7, 0.2, 0.4, 3.6, -2.4),
  year = c("2006", "2007", "2008", "2009", "2010"))

p <- ggplot(data = melt(data), aes(year, value, color = variable)) +
  geom_line(aes(group = variable)) +
  scale_color_hue(breaks = c("C", "I", "G", "X.M"),
                  labels = c("C", "I", "G", "X-M"))  # directlabels doesn't
                                                     # use this

# Compare:
p

# with:
direct.label(p, list(last.points, hjust = -0.25))

Resulting graphs can be seen here. The one with directlabels uses "X.M" instead of "X-M". Many thanks in advance!

  • 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-07T18:31:02+00:00Added an answer on June 7, 2026 at 6:31 pm

    The package directlabels seems to get the labels from the column names in your data.

    This means you have to make sure your labels are correct in the data to start with. To do this, you have to set check.names=FALSE when you create the data.frame:

    data = data.frame(
      C = c(1.2, 1.4, 0.3, -2.0, 0.5),
      I = c(1.2, 1.5, -1.3, -3.8, 1.8),
      G = c(0.2, 0.3, 0.3, 0.2, 0.2),
      "X-M" = c(2.9, -0.7, 0.3, -2.8, 1.5) +
        c(-2.7, 0.2, 0.4, 3.6, -2.4),
      year = c("2006", "2007", "2008", "2009", "2010"),
      check.names=FALSE)
    
    data
         C    I   G  X-M year
    1  1.2  1.2 0.2  0.2 2006
    2  1.4  1.5 0.3 -0.5 2007
    3  0.3 -1.3 0.3  0.7 2008
    4 -2.0 -3.8 0.2  0.8 2009
    5  0.5  1.8 0.2 -0.9 2010
    

    Now plot:

    p <- ggplot(data = melt(data), aes(year, value, color = variable)) +
      geom_line(aes(group = variable)) 
    direct.label(p, list(last.points, hjust = -0.25))
    

    enter image description here

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.