Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing x-axis tick labels using ggplot

Tags:

python

ggplot2

I am using ggplot to plot a histogram where the x variable is a categorical variable and I want to change the x-axis tick labels. Here is my code:

from pandas import *
from ggplot import *

df = pandas.read_csv('C:\Users\...csv')

def plot_data(df):

    plot = ggplot(data_by_group, aes('x', 'y')) +
           geom_histogram(stat='bar') + ggtitle('title') + 
           xlab('x-label') + ylab('y-label')  
    #x_ticklabels = ['a', 'b', 'c']    
    return plot

I would like to use the x_ticklabels on the x-axis instead of the numbers from the categorical variable.

Any ideas on how to do this?

Thank you

like image 350
Elena Forres Avatar asked Jul 07 '15 14:07

Elena Forres


People also ask

How do you change the x-axis tick label in R?

Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks. Option 2. Set axes = FALSE inside your plotting function to remove the plot box and add the new axes with the axis function.

How do I change the size of the x-axis labels in ggplot2?

To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element. This might be required when we want viewers to critically examine the X-axis labels and especially in situations when we change the scale for X-axis.

How do I extend the x-axis ticks in R?

To increase the width of axes tick (both X-axis and Y-axis at the same time) using ggplot2 in R, we can use theme function with axis. ticks argument where we can set element_line argument size to a larger value.


2 Answers

There is a good example here (under "Setting tick mark labels") showing how to do this. Briefly, given a ggplot "bp", you can control the actual tick labels by setting labels for each category you have in your data like this:

bp + scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"),
                  labels=c("Control", "Treat 1", "Treat 2"))

So in your case, I would imagine you would do something in the lines of

plot = ggplot(data_by_group, aes('x', 'y')) +
       geom_histogram(stat='bar') + ggtitle('title') + 
       xlab('x-label') + ylab('y-label') +
       scale_x_discrete(breaks=c(1, 2, 3),
                  labels=c("a", "b", "c"))
like image 129
MeloMCR Avatar answered Sep 30 '22 18:09

MeloMCR


Change the answer from MeloMCR as below to make it work:

    plot = ggplot(data_by_group, aes('x', 'y')) +
   geom_histogram(stat='bar') + ggtitle('title') + 
   xlab('x-label') + ylab('y-label') +
   scale_x_discrete(breaks=c(1, 2, 3),
              labels=c("a", "b", "c"))

to

    plot = ggplot(data_by_group, aes('x', 'y')) +
   geom_histogram(stat='bar') + ggtitle('title') + 
   xlab('x-label') + ylab('y-label') +
   scale_x_discrete(breaks= [1, 2, 3],
              labels= ["a", "b", "c"])

column specification (c(a1,a2,a3,....) is not identified by python.

like image 44
dv_math Avatar answered Sep 30 '22 16:09

dv_math