全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
1052 3
2022-11-14
acc_a <- c(0.8001, 0.8101, 0.8202, 0.8306, 0.8605, 0.8453, 0.8788, 0.8966)
nmi_a <- c(0.8002, 0.8103, 0.8232, 0.8406, 0.8505, 0.8400, 0.8701, 0.8506)
ari_a <- c(0.8003, 0.8133, 0.8242, 0.8206, 0.8105, 0.8100, 0.8601, 0.8306)
pair_a <- c(0, 1000, 2000, 3000, 4000, 5000, 6000, 7000)


acc_b <- c(0.9001, 0.9101, 0.9202, 0.9306, 0.9605, 0.9453, 0.9788, 0.9966)
nmi_b <- c(0.9002, 0.9103, 0.9232, 0.9406, 0.9505, 0.9400, 0.9701, 0.9506)
ari_b <- c(0.9003, 0.9133, 0.9242, 0.9206, 0.9105, 0.9100, 0.9601, 0.9306)
pair_b <- c(0, 1000, 2000, 3000, 4000, 5000, 6000, 7000)


我现有以上两组的数据集,想按照a为实线、b为虚线,大概按照附件下图的折线图,将a、b两组数据以实线、虚线进行区分,画在同一张图中,请问能详细指导下该如何编写代码吗?初接触R语言,感谢大佬讲解!! 211848ubofwrbo6ooroyuw.jpg


我单画一组a的代码如下,请问大佬要如何修改,添加组b并以虚线表示在一张图中呢?
acc <- c(0.8001, 0.8101, 0.8202, 0.8306, 0.8605, 0.8453, 0.8788, 0.8966)
nmi <- c(0.8002, 0.8103, 0.8232, 0.8406, 0.8505, 0.8400, 0.8701, 0.8506)
ari <- c(0.8003, 0.8133, 0.8242, 0.8206, 0.8105, 0.8100, 0.8601, 0.8306)
pair <- c(0, 1000, 2000, 3000, 4000, 5000, 6000, 7000)


library(tidyr)
library(ggplot2)


df <- data.frame(acc, ari, nmi, pair)
df <- df %>% pivot_longer(1:3, names_to = 'type', values_to = 'value')


ggplot(df, aes(x = pair, y = value, colour = type, shape = type)) +
  geom_point() + geom_line() + theme_bw() +
  theme(legend.title = element_blank(), legend.position = 'bottom') +
  xlab('No. of Pairwise Constrains') + ylab('')



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2022-11-15 20:55:24
尝试了一下,这应该是你想要的内容
复制代码


为什么不考虑用facet_wrap()分面呢
复制代码

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

#将基础图赋值为p,两边加上括号,表示赋值的同时也直接print图片显示。
p1 <- ggplot(data)+
    geom_line(aes(x = reorder(year, year), #见下面注释
                 y = WoS, group = 1),
             linetype = 1, cex = 0.8, color = "orange") +
    geom_point(
      aes(x = reorder(year, year), #见下面注释
          y = WoS),
      color = "orange2", size = 2, alpha = 0.8
    ) +
    labs(x=NULL,y='国际期刊发文量') + #自定义x、y轴、标题内容
    theme_test(base_size = 10)+ #主题基本大小
    theme(axis.text.x = element_text(angle = 45,hjust = 1),
          axis.text = element_text(color = 'black',face = 'bold'),
          plot.margin = margin(1,0.5,0.5,2.5,'cm'),
          panel.border = element_rect(size = 1),
          axis.title = element_text(face = 'bold', family = "Kai"),
          plot.title = element_text(face = 'bold',
                                    size=13,hjust = 0.5))
p1


(p2 <- p1+
    scale_y_continuous(expand = c(0,0),limits = c(0,2000),
                       sec.axis = sec_axis(~./10,
                                           name = 'CNKI',
                                           breaks = seq(0,200,20)))+
    geom_line(aes(x= reorder(year, year),
                  y= CNKI*10,
                  group=1),
              linetype=1,cex=0.8, color = "darkolivegreen3")+
    geom_point(aes(x= reorder(year, year),
                   y= CNKI*10),
               color='#589c47',size=2, alpha = 0.7)
)



# 添加图例
#先定义一个用来画框的数据框:
df <- data.frame(a=c(6,6,27,27),
                 b=c(2000,1900,1900,2000))

(p3 <- p2+
    annotate('segment',x=8,xend = 12, y=1950,yend = 1950,
             linetype=1,cex=0.8,color = "orange")+   
    annotate('text',x=10,y=1950,label='•',
             size=10,color='orange2', alpha = 0.8)+
    annotate('text',x=14,y=1950,label='WoS',
             fontface='bold',size=4)+
    annotate('segment',x=17,xend = 21,y=1950,yend = 1950,
             linetype=1,cex=0.8,color="darkolivegreen3")+
    annotate('text',x=19,y=1950,label='•',
             size=10,color='#589c47')+
    annotate('text',x=23,y=1950,label='WoS',
             fontface='bold',size=4.5)+
    geom_line(data = df,aes(a,b),cex=0.5))



df <- data.frame(x = LETTERS[1:10],
                 y = 1:10)
ggplot() +
  geom_star(data = df, aes(x = x, y = y, starshape = x, fill = x), size = 5) +
  theme_bw()
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群