楼主: snile
1119 3

[问答] 请问如何用R语言采用实线和虚线区分不同种类,并且画在同一张折线图! [推广有奖]

  • 0关注
  • 0粉丝

大专生

28%

还不是VIP/贵宾

-

威望
0
论坛币
19 个
通用积分
0.1198
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
426 点
帖子
23
精华
0
在线时间
46 小时
注册时间
2017-12-8
最后登录
2023-1-19

楼主
snile 发表于 2022-11-14 16:06:43 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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('')



二维码

扫码加我 拉你入群

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

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

关键词:R语言 折线图 如何用 ACC 画图 实虚

沙发
handdyyy 发表于 2022-11-15 20:55:24
尝试了一下,这应该是你想要的内容
  1. pair <- c(0, 1000, 2000, 3000, 4000, 5000, 6000, 7000)
  2. acc_a <- c(0.8001, 0.8101, 0.8202, 0.8306, 0.8605, 0.8453, 0.8788, 0.8966)
  3. nmi_a <- c(0.8002, 0.8103, 0.8232, 0.8406, 0.8505, 0.8400, 0.8701, 0.8506)
  4. ari_a <- c(0.8003, 0.8133, 0.8242, 0.8206, 0.8105, 0.8100, 0.8601, 0.8306)

  5. acc_b <- c(0.9001, 0.9101, 0.9202, 0.9306, 0.9605, 0.9453, 0.9788, 0.9966)
  6. nmi_b <- c(0.9002, 0.9103, 0.9232, 0.9406, 0.9505, 0.9400, 0.9701, 0.9506)
  7. ari_b <- c(0.9003, 0.9133, 0.9242, 0.9206, 0.9105, 0.9100, 0.9601, 0.9306)

  8. group<- c(rep("A",8),rep("B",8))
  9. acc<-c(acc_a,acc_b)
  10. nmi<-c(nmi_a,nmi_b)
  11. ari<-c(ari_a,ari_b)
  12. pair1<-c(pair,pair)

  13. temp<-data.frame(pair1,acc,nmi,ari,group)
  14. mdata<- melt(temp,id=c("pair1","group"))
  15. #head(mdata)
  16. p1<-ggplot(mdata,aes(x=pair1,y=value,fill=variable))
  17. p1+geom_line(aes(linetype=group,colour=variable))+geom_point(aes(colour=variable))
复制代码


为什么不考虑用facet_wrap()分面呢
  1. p1+geom_line(aes(colour=variable))+geom_point(aes(colour=variable))+facet_wrap(vars(group))
复制代码

藤椅
货币主义者的菲利普斯曲线 发表于 2022-11-17 16:06:42
#将基础图赋值为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()

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-20 22:31