1

所以我有以下图表:

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

如您所见,Y 轴显示/标记从 10 到 25 的数字,我需要它显示从 0 到 100,以 5 为单位。有
什么想法吗?谢谢!!

这是代码:

  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))

关于我的数据库的这些信息:

'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
4

1 回答 1

0

试试这个代码。您的per2变量介于 0-1 之间,因为它看起来像一个百分比。您可以启用limitsbreaks内部scale_y_continuous()以获得预期的输出。如果你想打破每 5 个单位,你可以使用seq()函数定义一个序列by=0.05。这里的代码:

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))

由于没有提供数据,这里有一个带有虚拟数据的可重复示例,以显示新元素的工作原理(请下次包含您的数据样本):

#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))

输出:

在此处输入图像描述

并使用与您类似的数据:

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))

输出:

在此处输入图像描述

您可以尝试任何自定义breaks选项。

于 2020-09-28T00:50:33.730 回答