使用ggplot创建平滑的直方图和轮廓图

如何解决使用ggplot创建平滑的直方图和轮廓图

我正在尝试使用ggplot创建一些平滑的直方图和轮廓图。

我的数据摘录/示例如下:

structure(list(Year = c(14L,14L,15L,16L,17L,18L,19L,20L,21L,22L,23L,24L,25L,26L,27L,28L,28L),Period = c(1L,3L,1L,2L,2L),Power = c(82,82,223.6,164,119,74.5,279.5,67,112,149,238.5,205,194,336,558.9,287,388,186.3,89.4,126.7,536.6,402,298,342.8,536,521.6,335.3,357.7,313,782.6,670.6,223.5,391,436,171.4,350,634,864.4,760,503.5,63.3,812,317,231,432,918,745.2,424.8,372.6,782,626,544,373,391.2,864,894,179,391.2),Span = c(12.8,11,17.9,14.5,12.9,7.5,11.13,14.3,7.8,11.7,12.8,8.5,13.3,14.9,12,9.4,15.95,16.74,22.2,23.4,23.72,11.9,14.4,9.7,8,14.55,9.1,8.11,9.5,20.73,22.8,38.4,14,26.5,30.48,15.5,14.17,10.1,14.8,15.62,14.05,15.24,12.24,27.2,8.84,22.86,7.7,9.8,15.93,13.08,15.21,8.94,9.6,10.8,13.72,8.9,26.72,25,11.58,17.3,12.5,12.1,12.09,15.3,9.08,17.75,15.15,27.4,22,13.7,10.3,22.76,22.25,17.25,14.15,20.4,11.35),Length = c(7.6,9,10.35,7.9,6.3,8.28,6.7,8.3,8.7,7.4,6.2,10.25,10.77,10.9,12.6,11.86,9.2,6.5,6.95,9.83,7.3,6.38,13.27,13.5,20.85,14.33,19.16,8.1,9.68,11.89,10.97,11.28,11.42,18.2,7.01,18.08,6.8,7.1,11.5,9.27,9.78,6.17,6.4,7.32,10.74,6.9,18.97,15.1,7.06,7.17,10.55,8.38,8.81,9.42,5.99,10.27,10.22,19.8,14.63,11.2,6.56,14.88,13.81,7,7.2,9.91,15,8.94),Weight = c(1070,830,2200,1946,1190,653,930,1575,676,920,1353,1550,888,1275,1537,1292,611,1350,1700,3312,4920,1510,3625,900,1665,1640,1081,625,932,1378,886,902,1070,5670,3636,12925,2107,4770,6060,1192,1900,1050,2155,1379,2858,3380,2290,2347,3308,2630,1333,10000,1351,6250,885,1531,1438,3820,1905,2646,1151,1266,2383,860,7983,6200,1484,567,1867,4350,1935,1823,2253,1487,2220,1244,2700,2280,3652,8165,5500,3568,1414,5875,5460,4310,1500,1795,1628,2449,6900,2102),Speed = c(105L,145L,135L,138L,140L,177L,113L,230L,175L,106L,170L,157L,183L,201L,209L,120L,152L,176L,190L,205L,196L,165L,146L,222L,159L,166L,158L,185L,226L,161L,251L,171L,206L,235L,245L,214L,180L,220L,237L,254L,169L,153L,261L,200L,246L,174L,319L,290L,233L,250L,255L,298L,198L,195L,300L,270L,297L,225L,212L,197L,296L),Range = c(400L,402L,500L,400L,350L,700L,525L,560L,550L,450L,600L,800L,547L,1770L,2365L,925L,1205L,580L,684L,563L,644L,885L,440L,557L,750L,3600L,805L,330L,628L,1640L,604L,1046L,1585L,650L,917L,515L,1110L,772L,1127L,850L,523L,900L,668L,1706L,1385L,1000L,902L,579L,1125L,1300L,660L,756L)),row.names = c(NA,100L
),class = "data.frame")

我有以下代码:

library(ggplot2)
data <- read.csv("data.csv")
data[,3:8] <- log10(data[,3:8])

# density plots
ggplot( data,aes( Power,group = Period,colour = Period ) ) + geom_density( aes( fill = Period ),alpha = 1 ) + ggtitle("All data")
ggplot( data,aes( Length,alpha = 1 ) + ggtitle("All data")


library(ggplot2)
data <- read.csv("data.csv")
data[,3:8])
ggplot( data,aes(Power,Weight ) )  + 
  geom_density_2d( ) + 
  geom_point( aes( colour = Period ),alpha = 3 ) + 
  theme( legend.position = "bottom")

ggplot( data,aes( Speed,Length ) )  + 
  geom_density_2d( ) + 
  geom_point( aes( colour = Period ),alpha = 3 ) + 
  theme( legend.position = "bottom")

此代码生成以下图:

enter image description here

enter image description here

enter image description here

enter image description here

如您所见,Period应该不存在1.5和2.5-仅存在1、2和3。而且,对于等高线图,我想说的是“句点”,而不是“颜色” ,但是与平滑直方图中使用的相同代码似乎无效。最后,是否有一种方法可以使标题居中,以使“所有数据”位于中间?

解决方法

此问题是由于在本质上是类别映射使用连续变量引起的。通过使用分类变量或因子变量进行此类映射,您将得到满意的结果。 最简单的方法是在数据集中强制该变量:

data <- data %>%
  mutate(Period = as.factor(Period))

您的图将按预期显示(并为透明密度图稍微降低了alpha值):

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-