Home > Enterprise >  Combine multiple plots in the same plot but display axes only in specific of them using ggplot2
Combine multiple plots in the same plot but display axes only in specific of them using ggplot2

Time:10-15

I have the dataframe below

d1_4b<-structure(list(conm = c("Apple Inc", "Apple Inc", "Apple Inc", 
"Apple Inc", "Apple Inc", "Apple Inc", "Apple Inc", "Apple Inc", 
"Apple Inc", "Apple Inc", "Apple Inc", "Intl Business Machines Corp", 
"Intl Business Machines Corp", "Intl Business Machines Corp", 
"Intl Business Machines Corp", "Intl Business Machines Corp", 
"Intl Business Machines Corp", "Intl Business Machines Corp", 
"Intl Business Machines Corp", "Intl Business Machines Corp", 
"Intl Business Machines Corp", "Intl Business Machines Corp", 
"Paypal Holdings Inc", "Paypal Holdings Inc", "Paypal Holdings Inc", 
"Paypal Holdings Inc", "Paypal Holdings Inc", "Paypal Holdings Inc", 
"Paypal Holdings Inc", "Paypal Holdings Inc", "Qualcomm Inc", 
"Qualcomm Inc", "Qualcomm Inc", "Qualcomm Inc", "Qualcomm Inc", 
"Qualcomm Inc", "Qualcomm Inc", "Qualcomm Inc", "Qualcomm Inc", 
"Qualcomm Inc", "Qualcomm Inc", "Amazon.com Inc", "Amazon.com Inc", 
"Amazon.com Inc", "Amazon.com Inc", "Amazon.com Inc", "Amazon.com Inc", 
"Amazon.com Inc", "Amazon.com Inc", "Amazon.com Inc", "Amazon.com Inc", 
"Amazon.com Inc", "Facebook Inc", "Facebook Inc", "Facebook Inc", 
"Facebook Inc", "Facebook Inc", "Facebook Inc", "Facebook Inc", 
"Facebook Inc", "Facebook Inc", "Facebook Inc", "Facebook Inc"
), pr_margin = structure(c(0.296159448064392, 0.328982253877634, 
0.373437779538426, 0.326230179626704, 0.330692852649142, 0.349699420234046, 
0.322077632257975, 0.308610415557902, 0.307376045282052, 0.293945590258827, 
0.281747809773601, 0.236074536151636, 0.240974222754312, 0.247342283292028, 
0.241270764202865, 0.246063819469141, 0.237616373668049, 0.205067567567568, 
0.195276665108227, 0.219132816524481, 0.219166007751436, 0.208964955175224, 
0.229522818492642, 0.222305295950156, 0.228914359861592, 0.21306032097399, 
0.210034469551896, 0.213772571354605, 0.208474004051317, 0.231984711475715, 
0.35938495132381, 0.408285243198681, 0.345431724282203, 0.344848387356229, 
0.346811643447729, 0.333372888730667, 0.326781013840537, 0.292152726959666, 
0.239222241773711, 0.243427109974425, 0.267912199162487, 0.0603438194363232, 
0.0419327329076273, 0.041494115528784, 0.0451163165529469, 0.043949746033173, 
0.0682578546997365, 0.079073734989374, 0.0737633949152733, 0.106562410095883, 
0.106380248251474, 0.101081685938083, 0.593211752786221, 0.560226354082458, 
0.232265671055217, 0.498348577235772, 0.499278036258623, 0.459839357429719, 
0.536724799189522, 0.57388138636755, 0.523532361474265, 0.491223107062534, 
0.459873204211016), label = "Operating Income Before Depreciation", format.stata = ".0g"), 
    datadate = structure(c(14882, 15247, 15613, 15978, 16343, 
    16708, 17074, 17439, 17804, 18169, 18535, 14974, 15339, 15705, 
    16070, 16435, 16800, 17166, 17531, 17896, 18261, 18627, 16070, 
    16435, 16800, 17166, 17531, 17896, 18261, 18627, 14882, 15247, 
    15613, 15978, 16343, 16708, 17074, 17439, 17804, 18169, 18535, 
    14974, 15339, 15705, 16070, 16435, 16800, 17166, 17531, 17896, 
    18261, 18627, 14974, 15339, 15705, 16070, 16435, 16800, 17166, 
    17531, 17896, 18261, 18627), label = "Data Date", format.stata = "%td", class = "Date")), row.names = c(NA, 
-63L), class = c("tbl_df", "tbl", "data.frame"))

and Im looking for a way to combine 4 line plots in the same plot but I want the dates to be displayed only in the lower plots and the y axis values only in the plots of the first column like in the image below:

enter image description here

I used the method delow with ggpubr package but I believe that there may be another method that can combine all of them as expected maybe without having to create each one separately.

library(ggpubr)
library(ggplot2)
# 3. Create a line plot
lp1 <- ggplot(subset(d1_4b,conm =='Amazon.com Inc'), aes(x = datadate, y = pr_margin))   
  geom_line() 
lp1
lp2 <- ggplot(subset(d1_4b,conm =='Facebook Inc'), aes(x = datadate, y = pr_margin))   
  geom_line() 
lp2
lp3 <- ggplot(subset(d1_4b,conm =='Apple Inc'), aes(x = datadate, y = pr_margin))   
  geom_line() 
lp3
lp4 <- ggplot(subset(d1_4b,conm =='Paypal Holdings Inc'), aes(x = datadate, y = pr_margin))   
  geom_line() 
lp4

figure <- ggarrange(lp1,lp2,lp3,lp4,
                    ncol = 2, nrow = 2)
figure

CodePudding user response:

we can use native ggplot2 syntax;

plot <- ggplot(d1_4b, aes(x = datadate, y = pr_margin))   
geom_line() 
facet_wrap(~conm) 
theme_minimal() 
scale_y_continuous(labels = scales::percent) 
ylab('Profit Margin') 
xlab('Fiscal Year')

plot

output;

enter image description here

  • Related