简体   繁体   中英

Adjust step scale with ggplot for slope graph in R

I'm drawing a slope graph with ggplot, but the labels get clustered together and are not shown properly because of the scale of the two axis. Any idea?

My code and the graph Is there any way to adjust step scale?

Thanks alot!

#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

Have you tried to set up a scale, for example x (but I think you should do it for y too)

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

where 0 - 100 is the range and 5 is the step size. You need to adjust these values according to your graph. Source

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