美文网首页
R画slope chart及代码

R画slope chart及代码

作者: gtt儿_生物信息学习 | 来源:发表于2019-10-26 09:56 被阅读0次

我们在想要展示我们的科研结果的时候经常会需要用到一些图,其中slope chart也是我们常用图之一,slope看起来复杂,但是也是可以用R画的,代码如下:

library(ggplot2)
library(scales)
theme_set(theme_classic())

# prep data
df <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/gdppercap.csv")
colnames(df) <- c("continent", "1952", "1957")
left_label <- paste(df$continent, round(df$`1952`),sep=", ")
right_label <- paste(df$continent, round(df$`1957`),sep=", ")
df$class <- ifelse((df$`1957` - df$`1952`) < 0, "red", "green")

# Plot
p <- ggplot(df) + geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) + 
                  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                  geom_vline(xintercept=2, linetype="dashed", size=.1) +
                  scale_color_manual(labels = c("Up", "Down"),
                                     values = c("green"="#00ba38", "red"="#f8766d")) +  # color of lines
                  labs(x="", y="Mean GdpPerCap") +  # Axis labels
                  xlim(.5, 2.5) + ylim(0,(1.1*(max(df$`1952`, df$`1957`))))  # X and Y axis limits

# Add texts
p <- p + geom_text(label=left_label, y=df$`1952`, x=rep(1, NROW(df)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=df$`1957`, x=rep(2, NROW(df)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="Time 1", x=1, y=1.1*(max(df$`1952`, df$`1957`)), hjust=1.2, size=5)  # title
p <- p + geom_text(label="Time 2", x=2, y=1.1*(max(df$`1952`, df$`1957`)), hjust=-0.1, size=5)  # title

# Minify theme
p + theme(panel.background = element_blank(),
           panel.grid = element_blank(),
           axis.ticks = element_blank(),
           axis.text.x = element_blank(),
           panel.border = element_blank(),
           plot.margin = unit(c(1,2,1,2), "cm"))
image.png

恭喜你,又学到了一个新技能。有任何问题,欢迎留言。
微信公众号:生物信息学习,后台回复slope,即可获得代码。

相关文章

  • R画slope chart及代码

    我们在想要展示我们的科研结果的时候经常会需要用到一些图,其中slope chart也是我们常用图之一,slope看...

  • R画dumbbell chart及代码

    我们在想要展示我们的科研成果的时候经常会需要用到一些图,其中dumbbell chart也是我们常用图之一,dum...

  • R语言作图——Slope chart(坡度图)

    原创:黄小仙 今天小仙给大家分享一下Slope chart(坡度图)的画法,我在paper中看到的图是这样的 这个...

  • R画scatterplot及代码

    微信公众号:生物信息学习 在数据分析中经常会需要画各种图,其中比较常见的图之一就是scatterplot,那用R如...

  • downhill

    downhill downhill n. the downward slope of a hill;a ski r...

  • 用R画circos图及代码

    微信公众号:生物信息学习 最近有朋友问我怎么画circos图,需求比较简单,附上简单的代码 如果没有这个包,请自行...

  • R语言绘制waffle chart

    R绘制waffle chart的小示例

  • R实战| 雷达图(Radar Chart)

    R实战| 雷达图(Radar Chart) 雷达图(radar chart),又称蜘蛛网图(spider plot...

  • 在线作图|如何绘制一张坡度图

    坡度图 坡度图(slope chart)因形似斜坡而得名,它可以展示某一个指标随着时间推移的变化情况,比较此指标在...

  • HelloChart--PieChartView(饼图)

    在XML中的定义: 控件实例化: chart属性设置: 设置PieChartData属性及为chart设置数据:

网友评论

      本文标题:R画slope chart及代码

      本文链接:https://www.haomeiwen.com/subject/qssyvctx.html