简体   繁体   中英

How do I edit the values of a line chart using ggplot2?

So I have the following chart:

https://i.stack.imgur.com/wo7uV.png

As you can see, the Y axis is showing/tagging numbers from 10 to 25, and I need it to display from 0 to 100, breaking by 5.
Any ideas? Thanks!!

This is the code:

  ggplot(tabla4, aes(x = Año, y = per2, colour = Género)) +
  geom_line(size=2) +
  geom_point(size = 4, shape = 21, fill = "white") +
  ylab("Personas que simpatizan con un partido político") +
  xlab("") + 
  theme_gray(base_size = 12) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1))

And this info about my database:

'data.frame':   10 obs. of  7 variables:
 $ Año     : num  2006 2008 2010 2012 2014 ...
 $ Género  : Factor w/ 2 levels "Hombre","Mujer": 1 1 1 1 1 2 2 2 2 2
 $ Simpatía: Factor w/ 2 levels "No","Sí": 2 2 2 2 2 2 2 2 2 2
 $ Freq    : num  188 150 91 88 80 196 164 124 131 116
 $ countT  : num  677 601 738 557 503 ...
 $ per     : num  27.8 25 12.3 15.8 15.9 23.8 19 10.3 13.5 11.7
 $ per2    : num  0.278 0.25 0.123 0.158 0.159 0.238 0.19 0.103 0.135 0.117

Try this code. Your per2 variable is between 0-1 as it looks like a percentage. You can enable limits and breaks inside scale_y_continuous() in order to obtain the expected output. If you want breaks each 5 units, you can define a sequence with seq() function using by=0.05 . Here the code:

library(ggplot2)
#Code
ggplot(tabla4, aes(x = Año, y = per2, colour = Género)) +
  geom_line(size=2) +
  geom_point(size = 4, shape = 21, fill = "white") +
  ylab("Personas que simpatizan con un partido político") +
  xlab("") + 
  theme_gray(base_size = 12) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1),
                     limits = c(0,1),
                     breaks = seq(0,1,by=0.05))

As no data is provided, here a little reproducible example with dummy data to show how the new elements work (Please next time include a sample of your data):

#Data
df <- data.frame(x=1:10,y=seq(0,0.5,length.out = 10))
#Plot
ggplot(df,aes(x=x,y=y))+
  geom_point()+
  scale_y_continuous(labels = scales::percent_format(accuracy = 1),
                     limits = c(0,1),
                     breaks = seq(0,1,by=0.05))

Output:

在此处输入图片说明

And using data similar to yours:

library(tidyverse)  
#Data 2
df <- data.frame(Año=c(2006,2008,2010,2012,2014),
                 Mujer=c(0.24,0.19,0.10,0.14,0.12),
                 Hombre=c(0.34, 0.29, 0.2, 0.24, 0.22))
#Plot
df %>% pivot_longer(-Año) %>%
  ggplot(aes(x = Año, y = value, colour = name)) +
  geom_line(size=2) +
  geom_point(size = 4, shape = 21, fill = "white") +
  ylab("Personas que simpatizan con un partido político") +
  xlab("") + 
  theme_gray(base_size = 12) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1),
                     limits = c(0,1),
                     breaks = seq(0,1,by=0.05))

Output:

在此处输入图片说明

You can try any customization on breaks option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM