logo
down
shadow

Alternate tick labels in grouped ggplot to avoid overlap in R


Alternate tick labels in grouped ggplot to avoid overlap in R

By : Geoffrey Leach
Date : November 21 2020, 07:01 PM
will help you I have grouped boxplots with ggplot , try to rotate you labels to have full information
code :
+ theme(axis.text.x = element_text(angle = 30, hjust = 1)) 
+ scale_x_discrete(breaks=c("1","3"), labels=c(...))
ggplot(dat, aes(x=season, y=value)) + 
  geom_boxplot(fill="grey50") + 
  facet_grid(~facet) + 
  theme_classic()+    
  theme(legend.position="top") +
  labs(x="", y="", title="") +
  guides(fill=F) +
  theme(panel.background = element_rect(fill="grey95"))+ 
  theme(axis.text.x = element_text(color=c(1,0,1,0))) 


Share : facebook icon twitter icon
R - ggplot maximum tick grouped by runnumber Netlogo

R - ggplot maximum tick grouped by runnumber Netlogo


By : user3086482
Date : March 29 2020, 07:55 AM
changing x-axis tick labels using ggplot

changing x-axis tick labels using ggplot


By : garybean
Date : March 29 2020, 07:55 AM
this one helps. 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:
code :
bp + scale_x_discrete(breaks=c("ctrl", "trt1", "trt2"),
                  labels=c("Control", "Treat 1", "Treat 2"))
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"))
Detect when matplotlib tick labels overlap

Detect when matplotlib tick labels overlap


By : user6773917
Date : March 29 2020, 07:55 AM
To fix this issue There is no easy way to determine if labels overlap.
A possible solution might be to decide upon rotating the label on the basis of the number of charaters in the string. If there are a lot of characters, chances are high that there would be overlapping labels.
code :
import matplotlib.pyplot as plt
import pandas as pd

index = ["Label 1", "Label 2", "Lorem ipsum dolor sit amet", "Duis ac vehicula leo, vitae sodales orci."]
df = pd.DataFrame([1, 2, 3, 4], columns=["Value"], index=index)
ax = df.plot(kind="bar", rot=0)

threshold = 30
for t in ax.get_xticklabels():
    if len(t.get_text()) > threshold:
        t.set_rotation(90)

plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
import pandas as pd

index = ["Label 1", "Label 2", "Lorem ipsum dolor sit amet", "Duis ac vehicula leo, vitae sodales orci."]
df = pd.DataFrame([1, 2, 3, 4], columns=["Value"], index=index)
ax = df.plot(kind="bar", rot=15)
plt.setp(ax.get_xticklabels(), ha="right")

plt.tight_layout()
plt.show()
Tick labels overlap in pandas bar chart

Tick labels overlap in pandas bar chart


By : user1067486
Date : March 29 2020, 07:55 AM
Hope that helps Pandas bar plots are categorical plots. They create one tick (+label) for each category. If the categories are dates and those dates are continuous one may aim at leaving certain dates out, e.g. to plot only every fifth category,
code :
ax = series.plot(kind="bar")
ax.set_xticklabels([t if not i%5 else "" for i,t in enumerate(ax.get_xticklabels())])
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates

index = pd.date_range("2018-01-26", "2018-05-05")
series = pd.Series(np.random.rayleigh(size=100), index=index)

plt.bar(series.index, series.values)
plt.gca().xaxis.set_major_locator(dates.MonthLocator())
plt.gca().xaxis.set_major_formatter(dates.DateFormatter("%b\n%Y"))
plt.show()
Two tick labels overlap in the plot with symlog scale

Two tick labels overlap in the plot with symlog scale


By : Ryanasto1
Date : March 29 2020, 07:55 AM
Any of those help As @ImportanceOfBeingErnest noted in the comments, you can simply set the ticks explicitly with
code :
plt.gca().set_xticks([0, .1, 1, 10])
ticks = plt.xticks()[0]
ticks[1] = ticks[0]
plt.xticks(ticks[1:])
Related Posts Related Posts :
  • How do I interpret rpart splits on factor variables when building classification trees in R?
  • How to better create stacked bar graphs with multiple variables from ggplot2?
  • Building all combinations of a vector - looking for a nicer way
  • Fetching comments from tweets of a single user in R
  • Replace space between two words with an underscore in a vector
  • NSE in tidyverse with select()
  • How to split a table by one column in R?
  • writing to a dataframe from a for-loop in R
  • How to create a data.frame with a unknown number of columns?
  • R dplyr's group_by consider empty groups as well
  • injection from R^d to [0,1]^d
  • Parsing a vector in R
  • What is 'n' column in the below output?
  • Lattice: multiple plots in one window?
  • R - If statement function to set value of new column
  • Avoiding seasonality assumption for stl() or decompose() in R
  • Cannot add new library path to .libPaths()
  • What's the higher-performance alternative to for-loops for subsetting data by group-id?
  • package ‘fpc’ is not available (for R version 3.5.3)
  • R - Convert CHR to Date
  • Get characters after and before a pattern match in R
  • Subtract even columns from odd ones in a data frame
  • Changing numbers in a string
  • Using && for character vectors in r
  • Why am I getting this New York output alone?
  • Saving H2o data frame
  • In R, when using named rows, can a sparse matrix column be added (concatenated) to another sparse matrix?
  • How to convert hourly data to 6/12/24 hourly data
  • Crosstab with multiple items
  • count words with for loop in R
  • Replace cos^..(...) by (cos(...))^
  • Is there a Pair-Wise PostHoc Comparisons for the Chi-Square Test in R?
  • How do I specify random factors in R?
  • ANOVA with 3 fixed factors in R
  • convert numbers written in words to numbers using R programming
  • In R, how can I look up a value in one column based on an interval in adjoining columns that includes values I want to m
  • Convert Date in YYYYMM to YYYY-MM-DD
  • grep does not show all the indices, only [1]1
  • Normalizing a dataframe having several conditions, R
  • What is the equivalent of "everything()" operator in "data.table"?
  • The colors in ggplot do not correspond to the colors in the legend
  • How to add to each column of dataframe value from named vector?
  • Losing data while saving csv character file
  • How to target arbitrary HTML attribute containing space in R?
  • Iterate over a column and find the sum
  • reference another column with tidyeval in filter
  • How to create a new variable by comparing two other variables
  • Retrieving elements that are occurring after adding a fixed number from a start element in a vector
  • Get total mean for variables nested in a list of lists of lists
  • Render Xaringan Rmd when using _site.yml file
  • Regex: matching multiple patterns and getting middle of string
  • removing for loop with sample function
  • R: scraping additional data after POST only works for first page
  • R - Add ticks on top of plot with levels in variable?
  • How to avoid factors in R when reading csv data
  • Meaning of :: syntax
  • How to get month and week of the month from year and week no in R?
  • Can you save R code files with .RData objects for version control?
  • How to connect points of two different plots to each other using ggplot in R?
  • Is there an R function for finding the value below specific value
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk