R 如何添加具有自定义打断散点图的LIM?

R 如何添加具有自定义打断散点图的LIM?,r,ggplot2,R,Ggplot2,我是R的初学者。我想定制x轴和y轴,使其每10^2(从10开始,继续100、1k、10k、100k,并以1M结束)中断一次 这是我的密码: axs <- ggplot(fatality, aes(x= confirmed, y= deaths, label=country))+ geom_point() + geom_text(aes(label=ifelse(deaths>24543,as.character(country),'')),hjust=-0.1,vjust=0)

我是R的初学者。我想定制x轴和y轴,使其每10^2(从10开始,继续100、1k、10k、100k,并以1M结束)中断一次

这是我的密码:

axs <- ggplot(fatality, aes(x= confirmed, y= deaths, label=country))+ geom_point() + geom_text(aes(label=ifelse(deaths>24543,as.character(country),'')),hjust=-0.1,vjust=0)
    > axs
    > axs + scale_x_continuous(breaks = c(10,1000,10000,100000,1000000)
axs 24543,as.character(country),“”),hjust=-0.1,vjust=0)
>axs
>axs+比例x连续(中断=c(10100010000100001000000)

谢谢

我想我可以使用
scales
包来使用
label\u number()
函数

axs <- ggplot(fatality, aes(x= confirmed, y= deaths, label=country))+ geom_point() + geom_text(aes(label=ifelse(deaths>24543,as.character(country),'')),hjust=-0.1,vjust=0)
    > axs
    > axs + scale_x_continuous(breaks = c(10,1000,10000,100000,1000000), labels = scales::label_number()) 
axs 24543,as.character(country),“”),hjust=-0.1,vjust=0)
>axs
>axs+scale\u x\u连续(中断=c(1010001000010000000000),标签=scales::标签号()

如果希望显示带有k和M后缀,可以将标签编号部分更改为
label\u number\u si

这是一个解决方案,它使用包
缩放
,但采用对数缩放,并格式化轴标签

首先加载必要的包,聚合数据,并用基本元素开始绘图

library(dplyr)
library(ggplot2)
library(scales)
library(ggrepel)

df %>%
  group_by(countryName) %>%
  summarise(deaths = sum(death),
            confirmed = sum(confirmed)) %>%
  filter(deaths > 0, confirmed > 0) %>%
  mutate(country = ifelse(deaths > 24543, countryName, "")) %>%
  ggplot(aes(confirmed, deaths)) +
  geom_point() -> axs
现在有两个情节

带有
geom\u文本的一个

axs + geom_text(aes(label = country, hjust = -0.1, vjust = 0)) +
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))

另一个使用
geom\u text\u repel
使文本标签彼此远离

axs + geom_text_repel(aes(label = country, hjust = -0.1, vjust = 0),
                      direction = "x") +
  scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x)))

资料 数据取自中的代码


df谢谢,但它仍然给continueSorry一个“+”作为输出,我漏掉了一个括号,我刚刚修正了答案。太好了!非常感谢。但是,“R:Error-in-UseMethod(“groupby”):applicated-to-a-object-of-class”我在应用代码的第一步时得到了这个错误:(@Ipek-Can-you-run
str(df)
并查看产生错误的列的类别是什么?类别“tbl\u df”、“tbl”和“data.frame:$country=chr////$confirmed=num///$death=num////$case\u detairy=chr@Ipek我刚刚重新运行了代码,像在帖子中一样再次下载了数据,无法重现错误。什么是
class(df$countryName)
?它看起来很完美。谢谢。如果您能提供一个最小的、可复制的示例()来帮助我们帮助您,那将非常有用。
df <- read.csv ('https://raw.githubusercontent.com/ulklc/covid19-timeseries/master/countryReport/raw/rawReport.csv')
df$day <- as.Date(df$day, "%Y/%m/%d")