为自定义stat_summary fun.data添加图例

时间:2018-10-10 12:43:43

标签: r ggplot2 violin-plot

如何为使用public partial class Property { private int _propertyId; private int _instructionId; private int _referenceNumber; private string _caseOwner; private int _amountBorrowed; private Address _securityAddress; private int _correspondenceAddressId; private int _securityAddressId; public Property() { Occupiers = new HashSet<Occupier>(); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Property(int propertyId, int instructionId, int reference, int amountBorrowed, JurisdictionTypes jurisdiction, FunderTypes funder, bool correspondenceAddressIsSecurityAddress, Address correspondenceAddress, Address securityAddress) { Occupiers = new HashSet<Occupier>(); PropertyId = propertyId; InstructionId = instructionId; ReferenceNumber = reference; CaseOwner = ""; AmountBorrowed = amountBorrowed; Jurisdiction = jurisdiction; Funder = funder; CorrespondenceAddressIsSecurityAddress = correspondenceAddressIsSecurityAddress; SecurityAddress = securityAddress; } [Key] public int PropertyId { get => this._propertyId; set => this._propertyId = Math.Abs(value); } public int InstructionId { get => this._instructionId; set => this._instructionId = Math.Abs(value); } public int CorrespondenceAddressId { get => this._correspondenceAddressId; set => this._correspondenceAddressId = Math.Abs(value); } public int SecurityAddressId { get => this._securityAddressId; set => this._securityAddressId = Math.Abs(value); } public int ReferenceNumber { get => this._referenceNumber; set => this._referenceNumber = Math.Abs(value); } [StringLength(3)] public string CaseOwner { get => this._caseOwner; set => this._caseOwner = value.Trim(); } public int AmountBorrowed { get => this._amountBorrowed; set => this._amountBorrowed = Math.Abs(value); } public TenureTypes Tenure { get; set; } public JurisdictionTypes Jurisdiction { get; set; } public FunderTypes Funder { get; set; } public bool CorrespondenceAddressIsSecurityAddress { get; set; } public virtual Address CorrespondenceAddress { get; set; } public virtual Address SecurityAddress { get => _securityAddress; set => _securityAddress = CorrespondenceAddressIsSecurityAddress ? null : value; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Occupier> Occupiers { get; set; } public virtual SolicitorInstruction SolicitorInstruction { get; set; } } 添加到绘图中的对象添加图例?

这里是一个例子:

CREATE TABLE Property
(
    PropertyId INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    InstructionId INT NOT NULL FOREIGN KEY REFERENCES SolicitorInstruction(InstructionId),
    CorrespondenceAddressId INT NOT NULL FOREIGN KEY REFERENCES SolicitorInstruction(InstructionId),
    SecurityAddressId INT NOT NULL FOREIGN KEY REFERENCES SolicitorInstruction(InstructionId),

    ReferenceNumber INT NOT NULL,
    CaseOwner VARCHAR(3),
    AmountBorrowed INT,

    --2 tenure types: 1 = Freehold and 2 = Leasehold
    Tenure INT,

    --1 for Scotland, 2 for E&W, 3 for NI
    Jurisdiction INT,

    --5 funder types: Standard, PIC, LT, JR, Partnership
    Funder INT,

    CorrespondenceAddressIsSecurityAddress BIT NOT NULL,

    CONSTRAINT CHK_Tenure CHECK (Tenure BETWEEN 1 AND 2),
    CONSTRAINT CHK_Jurisdiction CHECK (Jurisdiction BETWEEN 1 AND 3),
    CONSTRAINT CHK_Funder CHECK (Funder BETWEEN 1 AND 5),
)

enter image description here

我想添加一个描述性的图例,该图例解释每个小提琴图中心的线和点代表什么。

根据下面的“相关主题”,我的印象是可以通过在stat_summary中定义ToothGrowth$dose <- as.factor(ToothGrowth$dose) p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_violin(trim=FALSE) data_summary <- function(x) { m <- mean(x) ymin <- m-sd(x) ymax <- m+sd(x) return(c(y=m,ymin=ymin,ymax=ymax)) } p + stat_summary(fun.data=data_summary) ### Code from http://www.sthda.com/english/wiki/ggplot2-violin-plot-quick-start-guide-r-software-and-data-visualization 然后添加aes(shape="")来实现。但是我没有任何成功。

相关。 ggplot2 legend for stat_summary

1 个答案:

答案 0 :(得分:1)

这听起来像您了解其工作原理,将常量映射到某种美感,然后使用scale_*_manual()清除图例。

我认为在scale_shape_manual()中删除图例名称,并通过更改limits在图例中添加第二个框。我使用了c("Mean", "1 SD"),但您可以随便使用这些。

所需形状的数量由图例框的数量决定,因此我给values赋予了两个形状,第二个使用NA,因为图例中的第二个框应是没有点的线

最后,我使用override.aes()中的guide_legend()从第一个框中删除行。

p + stat_summary(fun.data=data_summary, aes(shape = "Mean")) +
     scale_shape_manual(name = NULL, 
                        limits = c("Mean", "1 SD"),
                        values = c(19, NA),
                        guide = guide_legend(override.aes = list(linetype = c(0, 1))))

enter image description here