ggplot2散点图中的离散x轴和连续y轴的自定义缩放

时间:2020-06-28 07:26:55

标签: r ggplot2

具有这样的数据框

library(ggplot2)
ddft=data.frame(month = c("03-2012","04-2012","05-2012","06-2012","07-2012", "08-2012","09-2012","10-2012","11-2012","12-2012","02-2013","03-2013"), frqp = c("12.3", "11.4","44.1","11.3", "1.2","15.1","35.1","12","14.1","15.1","15.1","42.1"))
ddft$month <- factor(ddft$month, levels = ddft$month) #maintain X-axis order
ggplot(ddft, aes(x=month, y=frqp)) + geom_point()

如何缩放y轴以显示最高的无逗号分隔值,例如,最高值i 44.1%,以使该数字为45,而其他一些在y轴上点的数字也没有逗号分隔?同样对于x轴,怎么可能只显示一次2012年和2013年一次,但图形中的点与月份中的点相同?

2 个答案:

答案 0 :(得分:1)

仅保留年份值(缩放x轴):

  1. 保留字符串中最后n个字符的功能

    lastfew <- function(x, n){
      x=as.character(x)
      substr(x, nchar(x)-n+1, nchar(x))
    }
    
  2. 在月列上应用该功能

     yearlabels=lastfew(ddft$month,4)
    
  3. 创建情节

     ggplot(ddft, aes(x=month, y=frqp)) + geom_point(aes(size=frqp))+ theme(legend.position="none") + scale_x_discrete(labels= yearlabels)
    

enter image description here

用于缩放y轴:

Frequency=as.numeric(as.character(ddft$frqp))
minima=as.integer(min(Frequency))-1
maxima=as.integer(max(Frequency))+1
step=5
ggplot(ddft, aes(x=month, y=Frequency)) + geom_point(aes(size=Frequency))+ theme(legend.position="none") + scale_x_discrete(labels= yearlabels) + scale_y_continuous(breaks = seq(minima, maxima, step))

Output as desired

将标签合并为条带:

ddft$yearlabels=yearlabels
ggplot(ddft, aes(x=month, y=Frequency)) + geom_point(aes(size=Frequency))+ theme(legend.position="none",axis.ticks.x=element_blank(),axis.title.x=element_blank(),axis.text.x=element_blank(),panel.spacing.x=unit(0.1, "lines"))  + scale_y_continuous(breaks = seq(minima, maxima, 5))+facet_wrap(~yearlabels,strip.position = "bottom")

Label merge

答案 1 :(得分:1)

此版本侧重于与一年中的数据点成比例的比例。面板间距可以通过'x = unit change'更改。

if (event is RefreshNoteListEvent) {
  yield initialState;
  List<NotesModel> notesList = await notesRepository.fetchNotes();
  print('Bloc: i have awaited fetching $notesList'); // this shows as the very first print statement, but should be last... argh!
  yield NoteListLoadedState(notesList: notesList);
}

label merged and x_units