R 在绘图2上合理地组织x个记号

R 在绘图2上合理地组织x个记号,r,ggplot2,R,Ggplot2,我从提供的测试代码中得到了此图: 我希望x记号能够合理地组织(进一步查看使用原始数据创建的图的图像会突出问题) 以下是一些可以用作示例的代码: ## Create some numbers for testing set.seed(123) Aboard <- sample(1:50,50) ## some years to use Years <- c(1931, 1931, 1931, 1934, 1934, 1934, 1934, 1937, 1937, 1937, 1

我从提供的测试代码中得到了此图:

我希望x记号能够合理地组织(进一步查看使用原始数据创建的图的图像会突出问题)

以下是一些可以用作示例的代码:

## Create some numbers for testing

set.seed(123)
Aboard <- sample(1:50,50)

## some years to use

Years <- c(1931, 1931, 1931, 1934, 1934, 1934, 1934, 1937, 1937, 1937, 1937, 1937, 1938, 1943, 1943, 1943, 1943, 1943, 1955, 1955, 1955, 1955, 1955, 1961, 1961, 1961, 1970, 1970, 1970, 1970, 1973, 1973, 1973, 1978, 1980, 1980, 1982, 1982, 1983, 1984, 1984, 1985, 1986, 1986, 1986, 1987, 1987, 1989, 1990, 1990)

df <- data.frame(Aboard, Years)

###############################################################################

## I WANT TO FIND THE SUM OF FOR EACH YEAR

## change years to factor variable, so that I have levels to work with.
df$Years <- factor(df$Years)

## blank vector to store sum values.
aboardYearTotal= c()


## iterate over the levels of the years vector.
for(y in levels(as.factor(df$Years))){
  ## I want to use an integer rather than a string
  y = as.numeric(y)
  ## for each level - find the sum of all Aboard values that correspond with it.
  ## I need to remove NA values as there are some.
  yy=sum(df$Aboard[df$Years==y], na.rm = TRUE)
  aboardYearTotal = c(aboardYearTotal, yy)
}

## I no longer need y, or yy
rm(y)
rm(yy)

###############################################################################

## Create plot using this variable

yearLevels <- levels(as.factor(df$Years))
aboardYears <- data.frame(yearLevels, aboardYearTotal)

## Create a plot of the data for total number aboard each year
p <- ggplot(aboardYears, aes(yearLevels, aboardYearTotal))
p + geom_point(aes(size = aboardYearTotal))
这是原始图,它突出了我在x轴上遇到的问题:


我愿意接受建议或建议,以获得更好的一般做法

不要将
年份
转换为因子。相反,将其保留为数字,并使用
stat\u summary
来处理总和

df <- data.frame(Aboard, Years)

ggplot(df, aes(Years, Aboard)) +
  stat_summary(fun.y=sum, geom="point", aes(size=..y..))

通过提供这些值的向量,可以将x轴打断设置为所需的任何值。例如:

ggplot(df, aes(Years, Aboard)) +
  stat_summary(fun.y=sum, geom="point", aes(size=..y..)) +
  scale_x_continuous(breaks=seq(1920, 2020, 20))
scale_x_continuous(breaks=seq(min(df$Years), max(df$Years)+6, 6))
ggplot(aggregate(Aboard ~ Years, df, sum), aes(Years, Aboard, size=Aboard)) +
  geom_point()

有时,您需要或希望在ggplot之外执行数据摘要操作。有很多选择。以下是几点:

基本R

df.summary = aggregate(Aboard ~ Years, df, sum)
tidyverse

library(tidyverse)

df.summary = df %>%
  group_by(Years) %>% 
  summarise(Aboard = sum(Aboard))
您甚至可以在绘制数据时动态执行此操作,而无需创建单独的摘要数据框。例如:

ggplot(df, aes(Years, Aboard)) +
  stat_summary(fun.y=sum, geom="point", aes(size=..y..)) +
  scale_x_continuous(breaks=seq(1920, 2020, 20))
scale_x_continuous(breaks=seq(min(df$Years), max(df$Years)+6, 6))
ggplot(aggregate(Aboard ~ Years, df, sum), aes(Years, Aboard, size=Aboard)) +
  geom_point()