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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:41:18+00:00 2026-05-21T15:41:18+00:00

I am quite new to ggplot2, so forgive me if this post is too

  • 0

I am quite new to ggplot2, so forgive me if this post is too stupid.
I used the following code to plot the data, but I am not able to get the style that I need for publication.

In the output, I need:

  1. a legend. In my data case, there is nothing after opts(legend.position="top") I have no idea why. And I also would like to split the legend into 3 columns like columns=3 in auto.key of lattice

  2. Colorize the bars using grey system (eg, fill=c("white","grey20","grey70"))according to factor pl, but it seems that I cannot change the style with scale_colour_manual

  3. turn around the labels on the x-axis into horizontal.

  4. maybe a y-axis? But,do you think it is necessary?

BTW, I have no idea how to prepare a figure for publication, so, any suggestion is very welcome!

library(ggplot2)
wt<-gl(3,4,108,labels=c("W30","W60","W90"))
pl<-gl(3,12,108,labels=c("P0","P1","P2"))
gp<-gl(3,36,108,labels=c("A","B","C"))
dat<-cbind(A=runif(108),B=runif(108,min=1,max=10),C=runif(108,min=100,max=200),D=runif(108,min=1000,max=1500))
dat.df<-data.frame(wt,pl,gp,dat)
dat.m<-melt(dat.df)
ggplot(dat.m,aes(x=wt,y=value,group=pl,facet=gp,fill=pl))+
        stat_summary(fun.y=mean,geom="bar",size=2,position="dodge")+
        stat_summary(fun.ymin=function(x)(mean(x)-sd(x)/sqrt(length(x))),geom="errorbar",
                    fun.ymax=function(x)(mean(x)+sd(x)/sqrt(length(x))),position="dodge")+
                    facet_grid(variable~facet,scale="free_y")+ opts(legend.position="top")+ 
                    scale_colour_manual(values = c("red", "blue", "green")) 

My output

  • 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-05-21T15:41:19+00:00Added an answer on May 21, 2026 at 3:41 pm

    Here are some pointers:

    1. To get a horizontal legend, use opts(legend.direction="horizontal")

    2. To change the fill of the bars, you have to specify scale_fill_manual(values=c("white", "grey20", "grey70")). In your example, you have correctly mapped fill to pl. The only missing step is to map the manual scale to fill, rather than colour. Colour generally refers to the outline of the bar, and fill refers to the inside of the bar.

    3. To rotate the angle of axis text, use opts(axis.text.x = theme_text(angle=45)). The default orientation is horizontal, so I use 45 degrees for illustration.

    4. I don’t know what you mean by “maybe a y-axis”. Perhaps you don’t want to display the y-axis, in which case you can suppress it by opts(axis.title.y = theme_blank())

    Note that your example was not reproducible, so I had to invent some data. You can make it easier for us to respond if you ensure your example is reproducible:

    • There is no data for year
    • There is a reference to trt in your data.frame
    • You set up data for grp but then refer to it as gp

    My code:

    dat.df <- data.frame(
        gp = gl(3, 36, 108, labels=c("A", "B", "C")),
        yr = sample(2000:2010, 108, replace=TRUE),
        A=runif(108),
        B=runif(108, min=1, max=10),
        C=runif(108, min=100, max=200),
        D=runif(108, min=1000, max=1500)
    )
    dat.m <- melt(dat.df)
    
    ggplot(dat.m, aes(x=wt, y=value, group=pl, facet=gp, fill=pl))+
        stat_summary(fun.y=mean, geom="bar", size=2, position="dodge")+
        stat_summary(fun.ymin=function(x)(mean(x)-sd(x)/sqrt(length(x))), geom="errorbar", 
            fun.ymax=function(x)(mean(x)+sd(x)/sqrt(length(x))), position="dodge")+#, position="dodge"
        facet_grid(variable~facet, scale="free_y")+
        scale_fill_manual(values=c("white", "grey20", "grey70")) +
        opts(
            legend.position="top", 
            legend.direction="horizontal",
            axis.text.x = theme_text(angle=45),
            axis.title.y = theme_blank()
        ) 
    

    enter image description here

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

Sidebar

Related Questions

Hi I am quite new to php but i have been following some tutorials
I do not have problem as such but I am quite new to Ruby.
I've seen this quite a few times while using Office Interop classes this.CustomXMLParts.Add(MyResources.Data, new
I run into this quite often where a new page is supposedly tested and
I'm quite new to programming, and I was wondering if there is a good
I'm quite new to subject of writting stored function for mySQL database, hence i'm
I'd like to call methods of my DAOs by AJAX. I'm quite new in
First of all, I must say that I am quite new to the API
Scrum is quite popular dev.process these days and often Project Manager suddenly gets new
Quite often in ANSI C code I can see parenthesis sorrounding a single return

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.