Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在geom_point点图中仅显示前10个类别和值?_R_Sorting_Ggplot2 - Fatal编程技术网

R 如何在geom_point点图中仅显示前10个类别和值?

R 如何在geom_point点图中仅显示前10个类别和值?,r,sorting,ggplot2,R,Sorting,Ggplot2,我想用点图来排列订购最多的产品,以降序显示它们的订购时间。但是有134个产品,所以点图被压扁了。所以我需要整理出图中的前10个产品,我如何编辑代码 以下是我的绘图数据: head(product_count) product order_times frozen juice 2 baby bath body care 7 Indian foods 7 bea

我想用点图来排列订购最多的产品,以降序显示它们的订购时间。但是有134个产品,所以点图被压扁了。所以我需要整理出图中的前10个产品,我如何编辑代码

以下是我的绘图数据:

head(product_count)

product                    order_times
frozen juice                    2
baby bath body care             7
Indian foods                    7
beauty                          8
bulk grains rice dried goods    8
代码:


实际点打印有134行,但我只想显示10行(前10行)

您可以使用
top\n
筛选前10个值,然后使用它在
ggplot
对象中打印

library(dplyr)
library(ggplot2)

df %>% 
  top_n(10, order_times) %>% 
  ggplot() + aes(product, order_times) + geom_point() 
或仅使用
ggplot2

ggplot(df[tail(order(df$order_times), 10), ], ) + 
     aes(product, order_times) + geom_point() 

类似于
df%%>%top\u n(10)%%>%ggplot()+aes(产品、订单时间)+geom\u point()
?产品计数中出现错误%>%top\u n(10)%%>%ggplot():找不到函数“%%>”,如果尚未安装,则需要执行
库(dplyr)
安装.packages(“dplyr”)
,啊哈,它成功了。非常棘手。
ggplot(df[tail(order(df$order_times), 10), ], ) + 
     aes(product, order_times) + geom_point()