0
graf4<-ggplot(DF,
     aes(x=variable,y=value,color=paese)) +
     geom_line(aes(linetype=paese,group=paese),size=2.5)+
     theme_bw()+
     labs(x="",y="")+
    scale_colour_manual(values=c("darkorange1","darkorchid1","darkgreen","steelblue3","maroon2","grey2","yellow4",
                               "burlywood4", "chocolate"))+
    scale_y_continuous(labels=fmt_dcimals(1))+

    theme(panel.grid.major = element_line(colour = "grey",size=0.1)) +
    theme(panel.grid.minor = element_line(colour = "white",linetype="dashed",size=0.1)) +
    theme(strip.text.y=element_text(angle=0))+
    theme(axis.text.x = element_text(angle=90,vjust=0.5,size=12),
    axis.text.y = element_text(size=12),
    legend.position = "bottom",
    legend.box="horizontal",
    legend.box.background=element_rect(),
    legend.title = element_blank(),
    legend.text=element_text(size=12))+
    guides(col=guide_legend(nrow=1,byrow=TRUE))
    print(graf4)

The code above work but my problem is that the tick (label) on x axis are too many and I would like to have less tick on the graph. For example have a breaks every 3 observation.

Note that VARIABLE isn't date format but character format. I dont want, for some other reason date format.

I tried with scale_x_discrete, scale_x_continuous and other instruction with parameters or not but I cant have on the x axis every 3 observation tick, for example.

I know that for numeric type on the x axis I cant set a breaks, but I don't know hot to set for character value type.

   Variable * value * paese 
#1  1999 q1    12      UK
#2  1999 q2    15      UK
#3  1999 q3    55      UK  
#4  1999 q4    67      UK  
#5  1999 q1    12      DE  
#6  1999 q2    15      DE  
#7  1999 q3    55      DE 
#8  1999 q4    67      DE
#9  2000 q1    33      UK
#10 2000 q2    23      UK
#11 2000 q3    65      UK
#12 2000 q4    34      UK
#13 2000 q1    33      DE
#14 2000 q2    23      DE
#15 2000 q3    65      DE
#16 2000 q4    34      DE
# ....  .   ..        
#45 2023 q1    23      UK    
#46 2023 q2    11      UK 
#47 2023 q3    23      UK 
#48 2023 q1    23      DE 
#49 2023 q2    11      DE 
#50 2023 q3    23      DE 
diego
  • 1
  • 3
  • Hello Diego. Could you please post your data in an easily reproducible format using e.g. `dput` (see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? I would suggest converting to date - maybe just for the plot - then use `scale_x_date()`. + Where's the function `fmt_decimals` from? – harre Feb 17 '23 at 11:35
  • this is the function: fmt_dcimals <- function(decimals=0){ function(x) format(x,nsmall = decimals,scientific = FALSE)} – diego Feb 17 '23 at 12:02

2 Answers2

1

Something like this perhaps?

library(tidyverse)

df <- data.frame(variable = rep(LETTERS[1:21], 2, each = 2),
                 value = round(rnorm(84, 10, 3), 0),
                 paese = c("UK", "US"))

df |> 
  ggplot(aes(variable, value)) +
  geom_line(aes(linetype = paese, group = paese)) +
  scale_x_discrete(breaks = unique(df$variable)[seq(1, length(unique(df$variable)), 3)])

Created on 2023-02-17 with reprex v2.0.2

Godrim
  • 449
  • 2
  • 10
0

You can pass a custom labelling function in scale_x_discrete via the labels = parameter. This allows you to make blank labels for any of the points on the x axis.

For example, suppose I wanted to only show every year instead of every quarter. I could look for the string "q1" inside each label, and if it is present, return just the year component. If "q1" is not present, we would return an empty string:

ggplot(DF, aes(Variable, value, color = paese)) +
  geom_line(aes(linetype = paese, group = paese), size = 2.5) +
  scale_x_discrete(labels = ~ifelse(grepl('q1', .x), sub(' q1', '', .x), ''),
                   name = NULL) +
  scale_colour_manual(values = c("darkorange1", "darkorchid1")) +
  scale_y_continuous(NULL, labels = fmt_dcimals(1)) +
  guides(col = guide_legend(nrow = 1, byrow = TRUE)) +
  theme_bw() +
  theme(panel.grid.major      = element_line(colour = "grey", size = 0.1),
        panel.grid.minor      = element_blank(), 
        strip.text.y          = element_text(angle = 0),
        axis.text.x           = element_text(size = 12, vjust = -1),
        axis.text.y           = element_text(size = 12),
        legend.position       = "bottom",
        legend.box            = "horizontal",
        legend.box.background = element_rect(),
        legend.title          = element_blank(),
        legend.text           = element_text(size = 12))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87