ggplot2入门大全(给图像加注解)

注解会在图像中添加元数据,元数据依然是数据,所以可以使用以下函数:
geom_text():可添加文本说明或者为点添加标签;
geom_rect():标注图形中你感兴趣的区域;
geom_line()、geom_path()、geom_segment():添加直线,可以在直线上绘制箭头;
geom_vline()、geom_hline()、geom_abline():在图形中添加参照线(基准线),其跨度为整张图像。
一般来说,注解既可添加在前景(必要时使用alpha函数以保证能看清数据)也可添加在背景中。添加于背景时,默认使用白色字体。
下面依然使用economics数据集举例说明:
添加注解标明各个总统的当政时间

library(ggplot2)
ggplot(economics,aes(date,unemploy))+geom_line()
presidential <- subset(presidential,start > economics$date[1])
ggplot(economics)+
  geom_rect(aes(xmin=start,xmax=end,fill=party),
  ymin=-Inf,ymax=Inf,alpha=0.2,data=presidential)+
  geom_vline(aes(xintercept=as.numeric(start)),
  data=presidential,colour="grey50",alpha=0.5)+
  geom_text(aes(x=start,y=2500,label=name),
  data=presidential,size=3,vjust=0,hjust=0,nudge_x=50)
  geom_line(aes(date,unemploy))+
  scale_fill_manual(values = c("blue","red"))

总统当政时间
图看起来还不错,但好像有些繁琐。

各类注解,尤其是参照线,在通过分面比较组间关系时格外有用,不过其实用的也不多。。

ggplot(diamonds,aes(log10(carat),log10(price)))+
  geom_bin2d()+facet_wrap(~cut,nrow = 1)

在这里插入图片描述

mod_coef <- coef(lm(log10(price)~log10(carat),
                    data=diamonds))
ggplot(diamonds,aes(log10(carat),log10(price)))+
  geom_bin2d()+geom_abline(intercept = mod_coef[1],
  slope=mod_coef[2],colour="white",size=1)+
  facet_wrap(~cut,nrow = 1)

在这里插入图片描述

预告:下一篇介绍群组几何对象
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用Spring AOP的注解和切面来实现。首先,使用@Aspect注解定义一个切面类,该类包含一个或多个带有@Before、@After或@Around注解的方法,用于拦截在特定位置执行的方法。然后,使用@Pointcut注解定义一个切点,该切点定义了要拦截的方法的位置。最后,使用@Order注解定义切面的优先级,以确保它们按正确的顺序执行。 例如,以下代码演示了如何使用Spring AOP的注解和切面来拦截带有@MyAnnotation注解的方法,并在方法执行前后打印日志: ```java @Aspect @Component public class MyAspect { @Pointcut("@annotation(com.example.MyAnnotation)") public void myAnnotationPointcut() {} @Before("myAnnotationPointcut()") public void beforeMyAnnotation() { System.out.println("Before MyAnnotation method"); } @After("myAnnotationPointcut()") public void afterMyAnnotation() { System.out.println("After MyAnnotation method"); } @Order(1) @Around("myAnnotationPointcut()") public Object aroundMyAnnotation(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Around MyAnnotation method"); Object result = joinPoint.proceed(); return result; } } ``` 在上面的代码中,@Pointcut注解定义了一个切点myAnnotationPointcut(),用于拦截所有带有@MyAnnotation注解的方法。@Before和@After注解分别定义了在方法执行前和执行后要执行的方法。@Around注解定义了一个环绕通知方法,它包装了被拦截的方法,允许在方法执行前后执行任意代码。最后,使用@Order注解指定了切面的优先级。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值