使用ggplot2将文本注释到x轴

时间:2016-06-08 08:45:45

标签: r ggplot2

考虑一个简单的例子:

External component has thrown an exception.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.SEHException: External component has thrown an exception.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SEHException (0x80004005): External component has thrown an exception.]
   System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
   System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +485
   System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) +190
   System.Reflection.Assembly.LoadFrom(String assemblyFile) +54
   WebActivator.ActivationManager.get_Assemblies() +244
   WebActivator.ActivationManager.RunActivationMethods() +86
   WebActivator.ActivationManager.RunPreStartMethods() +46
   WebActivator.ActivationManager.Run() +68

[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: External component has thrown an exception..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +900
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +164
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +169
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +172
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +907

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: External component has thrown an exception..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +118
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +726

现在,我们可以使用以下内容获取library(ggplot2) head(mtcars) # create the plot ggplot(mtcars, aes(factor(cyl))) + geom_bar() + theme_bw() + theme(strip.text.x = element_text(size = 20, face="bold"))+ xlab("number of cyl") + ylab("Count") $mpg的平均值:

cyl

如何将这些平均值放入x轴,使它们显示在相应的aggregate(mpg ~ cyl, data = mtcars, FUN=mean) 下方。可以画一张桌子并以某种方式写出这是......每平均mpg ......

1 个答案:

答案 0 :(得分:1)

这是通过重写因子级别名称来实现的一种简单方法:

(请注意,只要aggregate以与因子级别名称相同的顺序生成表格并且没有任何间隙,这是安全的 - 这似乎应该是这种情况,但是人们必须进行调查确保。将它编码为循环并查看级别名称以确保它们正确匹配可能更安全。

library(ggplot2)

head(mtcars)

adf <- aggregate(mpg ~ cyl, data = mtcars, FUN=mean)
mtcars$fcyl <- factor(mtcars$cyl)
levels(mtcars$fcyl) <- sprintf("%d \n %.1f",adf$cyl,adf$mpg)

# create the plot
ggplot(mtcars, aes(fcyl)) + geom_bar() + theme_bw() +
  theme(strip.text.x = element_text(size = 20, face="bold"))+
  xlab("number of cyl") + ylab("Count") 

得到以下特性:

enter image description here