如何在方框图中清楚显示变量名称

时间:2018-07-31 08:25:13

标签: r

我需要看到下图中写的变量名,因为您可以看到变量名是以垂直方式编写的,以防止读者看到它们。

enter image description here

我用来绘制箱线图的代码是:

Data<-data.frame(hierarchical,df)
frameDataClusterGrpe1<-Data[which(Data$hierarchical==1),]
boxplot(frameDataClusterGrpe1,las=2)

为了使我的示例具有可重复性,这是数据集的一部分:

    > dput(Data[1:10,])
structure(list(hierarchical = c(1L, 2L, 3L, 3L, 2L, 1L, 1L, 1L, 
1L, 3L), TYPE_PEAU = c(-0.57288314920011, -0.57288314920011, 
0.575705233678928, -0.57288314920011, -0.57288314920011, -0.57288314920011, 
-0.57288314920011, 1.72429361655797, 0.575705233678928, -0.57288314920011
), SENSIBILITE = c(0.778411858486852, -0.611121590654152, 0.778411858486852, 
0.778411858486852, 0.778411858486852, -2.00065503979516, 0.778411858486852, 
0.778411858486852, 0.778411858486852, 0.778411858486852), IMPERFECTIONS = c(0.153668403847404, 
0.153668403847404, 1.71724441299474, 1.71724441299474, -1.40990760529994, 
0.153668403847404, 0.153668403847404, 1.71724441299474, 0.153668403847404, 
0.153668403847404), BRILLANCE = c(0.611341816486383, 0.611341816486383, 
-1.72495977327848, 0.611341816486383, -1.72495977327848, 0.611341816486383, 
0.611341816486383, -1.72495977327848, -1.72495977327848, 0.611341816486383
), GRAIN_PEAU = c(0.721013947110648, 0.721013947110648, 0.721013947110648, 
-1.66478017056442, 0.721013947110648, 0.721013947110648, 0.721013947110648, 
-0.471883111726887, 0.721013947110648, -0.471883111726887), RIDES_VISAGE = c(0.76160293927347, 
-1.3761377247562, -1.3761377247562, 0.76160293927347, -1.3761377247562, 
0.76160293927347, 0.76160293927347, 0.76160293927347, 0.76160293927347, 
0.76160293927347), MAINS = c(-0.821850698542963, -0.821850698542963, 
0.859019825612744, 0.859019825612744, -2.50272122269867, -2.50272122269867, 
-2.50272122269867, 0.859019825612744, 0.859019825612744, 0.859019825612744
), PEAU_CORPS = c(0.138519340680625, 0.138519340680625, 0.138519340680625, 
0.138519340680625, 0.138519340680625, 0.138519340680625, 0.138519340680625, 
0.138519340680625, 0.138519340680625, 1.62213438428626), INTERET_ALIM_NATURELLE = c(-0.511256705293382, 
-0.511256705293382, 1.96590375964003, -0.511256705293382, -0.511256705293382, 
-0.511256705293382, -0.511256705293382, -0.511256705293382, -0.511256705293382, 
1.96590375964003), INTERET_ORIGINE_GEO = c(-0.615549348507463, 
-0.615549348507463, 0.523216946231343, 1.66198324097015, -0.615549348507463, 
-0.615549348507463, -0.615549348507463, -0.615549348507463, -0.615549348507463, 
1.66198324097015), INTERET_VACANCES = c(-0.674641038733191, 0.921747930827325, 
2.51813690038784, -0.674641038733191, -0.674641038733191, -0.674641038733191, 
-0.674641038733191, 0.921747930827325, 0.921747930827325, -0.674641038733191
), INTERET_ENVIRONNEMENT = c(-0.453247144592726, 2.22025412858466, 
2.22025412858466, 2.22025412858466, -0.453247144592726, -0.453247144592726, 
-0.453247144592726, -0.453247144592726, -0.453247144592726, 2.22025412858466
), AGE_INTERVAL = c(0.113315170554397, 0.113315170554397, 1.23817552215537, 
-1.01154518104657, -1.01154518104657, 0.113315170554397, 0.113315170554397, 
1.23817552215537, 1.23817552215537, 0.113315170554397), ATTENTE_BEAUTE_1 = c(-1.37923941425187, 
1.10241594207223, 0.10975379954259, 0.10975379954259, 1.10241594207223, 
1.10241594207223, -0.386577271722232, -1.37923941425187, -1.37923941425187, 
0.10975379954259), ATTENTE_BEAUTE_2 = c(-0.694553905243566, -0.694553905243566, 
-0.171066054439619, 1.39939749797222, 0.352421796364328, -1.21804175604751, 
0.352421796364328, 1.92288534877617, -0.694553905243566, -0.694553905243566
), MILIEU_VIE = c(-1.73846454742541, -1.73846454742541, 0.573806925784203, 
0.573806925784203, 0.573806925784203, 0.573806925784203, -1.73846454742541, 
-1.73846454742541, 0.573806925784203, 0.573806925784203), PROFIL_SELECTIONNE = c(-1.52468205833385, 
1.01853760454269, 0.11610482094134, 0.280183508868859, 1.18261629247021, 
0.936498260578933, -0.376131242841216, -1.11448533851505, -1.52468205833385, 
0.0340654769775807), NOMBRE_ACHAT = c(2.15193684769553, -0.0267653836778052, 
-0.843778720442807, 0.790247953087196, 0.517910174165529, 1.87959906877387, 
1.06258573200886, 2.15193684769553, -0.57144094152114, -0.843778720442807
), NOMBRE_CADEAU = c(1.27279390361087, -0.532178635656112, -0.532178635656112, 
-0.532178635656112, 1.27279390361087, 1.27279390361087, -0.532178635656112, 
1.27279390361087, -0.532178635656112, -0.532178635656112)), .Names = c("hierarchical", 
"TYPE_PEAU", "SENSIBILITE", "IMPERFECTIONS", "BRILLANCE", "GRAIN_PEAU", 
"RIDES_VISAGE", "MAINS", "PEAU_CORPS", "INTERET_ALIM_NATURELLE", 
"INTERET_ORIGINE_GEO", "INTERET_VACANCES", "INTERET_ENVIRONNEMENT", 
"AGE_INTERVAL", "ATTENTE_BEAUTE_1", "ATTENTE_BEAUTE_2", "MILIEU_VIE", 
"PROFIL_SELECTIONNE", "NOMBRE_ACHAT", "NOMBRE_CADEAU"), row.names = c(NA, 
10L), class = "data.frame")

谢谢您的帮助!

4 个答案:

答案 0 :(得分:3)

尝试使用par增加箱形图页边距,并用cex.axis弄乱字体大小:

boxplot(d,las=2, par(mar = c(13, 5, 4, 2)+ 0.1), cex.axis=0.9)

答案 1 :(得分:2)

您需要使用class PayslipPostAPIView(APIView): """ .. http:get:: /payslip_create/ **Request**: .. sourcecode:: http GET /payslip_create/web HTTP/1.1 Host: {{theDomain}} Accept: application/json, text/javascript **Response**: .. sourcecode:: http HTTP/1.1 200 OK Vary: Accept Content-Type: text/javascript [ { "id": 123, "employee": 123, "basic_salary": ["server", "web"], "payment_mode": "I tried Nginx", "currency": "Currency", "payslip_no": "Payslip Number", "month_ending": "Date", "is_accounted": "True/False", "created_at": "Date" "modified_at": "Date", "total_allowances": 123, "total_deductions": 123, "net_pay": 123, "organization": 123 } ] :reqheader Authorization: JWT token required to authenticate .. http:post:: /payslip_create/ :param post_id: post's unique id :type post_id: int :reqheader Authorization: JWT token required to authenticate :status 201: Payslip successfully Created :status 400: Post parameters are invalid or missing """ permission_classes = (permissions.DjangoObjectPermissions,) queryset = Payslip.objects.all() model_list = [EmployeeProfile, Salary, Allowance, Deduction, PaymentMode] def get(self, request, format=None): payslip = Payslip.objects.filter(organization=get_auth(request)).all() serializer = PayslipSerializer(payslip, many=True) return Response(serializer.data) def post(self, request, format=None): try: allowance_list = request.data.pop('allowance_list') deduction_list = request.data.pop('deduction_list') # employee = EmployeeProfile.objects.get( # id=request.data.pop('employee'), organization=get_auth(request)) employee = EmployeeProfile.objects.get(id=request.data.pop('employee'), organization=get_auth(request)) basic_salary = Salary.objects.get( employee=employee) # print(basic_salary) payment_mode = PaymentMode.objects.get( id=request.data.pop('payment_mode'), organization=get_auth(request)) data = request.data data["organization"] = get_auth(request) payslip = Payslip.objects.create(**data) payslip.payslip_no = "PAYSLIP" + str(payslip.id) + "DT" + \ str(datetime.datetime.today()) payslip.basic_salary = basic_salary payslip.employee = employee payslip.payment_mode = payment_mode payslip.save() for item in allowance_list: allowance = Allowance.objects.create(name=AllowanceType.objects.get(id=item['name']), amount=item['amount'], payslip=payslip, organization=get_auth( request) ) for item in deduction_list: ded = DeductionType.objects.get(id=item['name']) deduction = Deduction.objects.create(name=ded, amount=item['amount'], payslip=payslip, organization=get_auth( request) ) serializer = PayslipSerializer(payslip) allow = Allowance.objects.filter( payslip=payslip.id).aggregate(Sum('amount')) deduct = Deduction.objects.filter( payslip=payslip.id).aggregate(amount=Sum('amount')) total_deductions = deduct['amount'] total_allowances = allow['amount__sum'] net_pay = (payslip.basic_salary.salary_value + allow['amount__sum']) - deduct['amount'] payslip.total_deductions = total_deductions payslip.total_allowances = total_allowances payslip.basic_salary = basic_salary payslip.organization = get_auth(request) payslip.net_pay = net_pay payslip.save() serializer = PayslipSerializer(payslip) payload = { "allowances": list(Allowance.objects.filter(payslip=payslip.id, organization=get_auth(request)).values('name', 'amount', 'name__name')), "deductions": list(Deduction.objects.filter(payslip=payslip.id, organization=get_auth(request)).values('name', 'amount', 'name__name')), "app": "payroll", "organization": get_auth(request), "total_deductions": total_deductions, "basic": payslip.basic_salary.salary_value, "sal_grp": payslip.basic_salary.salary_group.name, "total_allowances": total_allowances, "net_pay": net_pay, "paid": payslip.is_paid, "data": json.dumps(serializer.data) } # print(payload) ############################################### # Post the paslip entry updates to accounting # ############################################### # # ACC_IP = "http://0.0.0.0:8000" accounting_url = ACC_IP + "/acc/incoming/transations/" try: if send_payslip_accounts(payload, accounting_url): payslip.is_accounted = True payslip.save() return Response(data=json.dumps(serializer.data), status=status.HTTP_201_CREATED) else: payslip.is_accounted = False payslip.save() except Exception as e: return Response(data=str(e), status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_201_CREATED) except Exception as e: raise e return Response(serializer.data, status=status.HTTP_201_CREATED) 命令来调整其大小。

对于x轴,必需的命令是

例如

par

如果您还需要调整y轴的大小,

par(cex.axis = 0.6)

希望这会有所帮助。

答案 2 :(得分:2)

您可以使用制图参数更改边距(请参见?par中的更多内容)。 您可以使用> par(mar=c(a,b,c,d),mgp=c(x,y,z)) 并在字母中填入最适合您的情况的值。 然后剩下的就是> boxplot(data,las=2) ,其中las = 2可以确保您的标签垂直于轴(即,测试是从下至上,而不是从左至右)

答案 3 :(得分:2)

要更好地控制绘图元素,您可能希望使用ggplot2软件包。此软件包还有很多很棒的教程和小插图(例如thisthis)。

library(ggplot2)
library(reshape2)

melt<-melt(frameDataClusterGrpe1)

ggplot(melt, aes(x=variable, y=value))+
  geom_boxplot()+
  theme(axis.text.x = element_text(size=8, angle=45, vjust=0.8, hjust=0.8))

enter image description here