【问题标题】:Adjust step scale with ggplot for slope graph in R使用 ggplot 调整 R 中斜率图的步长比例
【发布时间】:2017-06-14 12:25:44
【问题描述】:

我正在使用 ggplot 绘制斜率图,但标签会聚集在一起并且由于两个轴的比例而无法正确显示。 有什么想法吗?

我的代码和图表Is there any way to adjust step scale?

非常感谢!

#Read file as numeric data
betterlife<-read.csv("betterlife.csv",skip=4,stringsAsFactors = F)
num_data <- data.frame(data.matrix(betterlife))
numeric_columns <- sapply(num_data,function(x){mean(as.numeric(is.na(x)))<0.5})
final_data <- data.frame(num_data[,numeric_columns], 
betterlife[,!numeric_columns])

## rescale selected columns data frame
final_data <- data.frame(lapply(final_data[,c(3,4,5,6,7,10,11)], function(x) scale(x, center = FALSE, scale = max(x, na.rm = TRUE)/100)))

## Add country names as indicator
final_data["INDICATOR"] <- NA 
final_data$INDICATOR <- betterlife$INDICATOR
employment.data <- final_data[5:30,]
indicator <- employment.data$INDICATOR
## Melt data to draw graph
employment.melt <- melt(employment.data)


#plot
sg = ggplot(employment.melt, aes(factor(variable), value, 
                 group = indicator, 
                 colour = indicator, 
                 label = indicator)) +
  theme(legend.position = "none", 
    axis.text.x = element_text(size=5),
    axis.text.y=element_blank(), 
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    axis.ticks=element_blank(),
    axis.line=element_blank(),
    panel.grid.major.x = element_line("black", size = 0.1),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.background = element_blank())
# plot the right-most labels

sg1 = sg + geom_line(size=0.15) + 
  geom_text(data = subset(employment.melt, variable == "Life.expectancy"), 
        aes(x = factor(variable), label=sprintf(" %2f %s",value,INDICATOR)), size = 1.75, hjust = 0) 

# plot the left-most labels

sg1 = sg1 + geom_text(data = subset(employment.melt, variable == "Employment.rate"), 
                 aes(x = factor(variable), label=sprintf("%s %2f ",INDICATOR,value)), size = 1.75, hjust = 1)
sg1

【问题讨论】:

  • “调整步长”是什么意思? PS:欢迎来到本站!
  • 假设我想为 99 和 99.5 的值添加标签,我想设置两个标签之间的最小距离,使它们不重叠(如图所示)。告诉我这是否有意义。谢谢!

标签: r graph ggplot2 rstudio scale


【解决方案1】:

您是否尝试过设置比例,例如 x(但我认为您也应该为 y 设置)

scale_x_continuous(breaks = seq(0, 100, 5))

其中 0 - 100 是范围,5 是步长。您需要根据您的图表调整这些值。 Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多