我正在使用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
答案 0 :(得分:0)
您是否尝试过设置比例,例如x(但我认为您也应该为y设置)
scale_x_continuous(breaks = seq(0, 100, 5))
其中0 - 100是范围,5是步长。您需要根据图表调整这些值。 Source