htmlrdataframesjplotitalic

Creating a data frame that produces partially italicized cells with pkg:sjPlot functions


I am attempting to create a simple data table in which the genus name in the "Coral_taxon" column is italicized while the "spp." Portion following the genus name is not capitalized. I attempted to code each row for "Coral_taxon" using the expression() function but have had no success.

sum_dat_final2 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", "Waikiki", "Waikiki", "Waikiki"), 
                                 Coral_taxon = c(expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp."), expression(italic("Montipora"), " spp."), expression(italic("Pocillopora"), " spp."), expression(italic("Porites"), " spp.")),
                                 Estimate = c("0.45 ± 0.13 %", "0.43 ± 0.18 %", "19.5 ± 2.6 %", "0.05 ± 0.03 %", "0.47 ± 0.3 %", "1.19 ± 0.56 %"))
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"expression"’ to a data.frame

enter image description here


Solution

  • sjPlot library uses HTML tags. You can use <i>Text</i> for italicizing.

    df1 <- data.frame(Site = c("Hanauma Bay", "Hanauma Bay", "Hanauma Bay", 
                               "Waikiki", "Waikiki", "Waikiki"),
                      Coral_taxon = I(list("<i>Montipora</i> spp.", 
                                           "<i>Pocillopora</i> spp.", 
                                           "<i>Porites</i> spp.", 
                                           "<i>Montipora</i> spp.", 
                                           "<i>Pocillopora</i> spp.", 
                                           "<i>Porites</i> spp.")),
                      Estimate = c("0.45 <U+623C><U+3E31> 0.13 %", "0.43 <U+623C><U+3E31> 0.18 %", 
                                   "19.5 <U+623C><U+3E31> 2.6 %", "0.05 <U+623C><U+3E31> 0.03 %", 
                                   "0.47 <U+623C><U+3E31> 0.3 %", "1.19 <U+623C><U+3E31> 0.56 %"))  
    
    sjPlot::tab_df(df1)
    

    Run the snippet to see the formatted table.

        <table style="border-collapse:collapse; border:none;">
    
        <tr>
    
        <th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; text-align:left; ">
    
        Site
    
        </th>
    
        <th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">
    
        Coral_taxon
    
        </th>
    
        <th style="border-top: double; text-align:center; font-style:italic; font-weight:normal; padding:0.2cm; border-bottom:1px solid black; ">
    
        Estimate
    
        </th>
    
        </tr>
    
        <tr>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
    
        Hanauma Bay
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        <i>Montipora</i> spp.
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        0.45 ±0.13 %
    
        </td>
    
        </tr>
    
        <tr>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
    
        Hanauma Bay
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        <i>Pocillopora</i> spp.
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        0.43 ±0.18 %
    
        </td>
    
        </tr>
    
        <tr>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
    
        Hanauma Bay
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        <i>Porites</i> spp.
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        19.5 ±2.6 %
    
        </td>
    
        </tr>
    
        <tr>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
    
        Waikiki
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        <i>Montipora</i> spp.
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        0.05 ±0.03 %
    
        </td>
    
        </tr>
    
        <tr>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">
    
        Waikiki
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        <i>Pocillopora</i> spp.
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">
    
        0.47 ±0.3 %
    
        </td>
    
        </tr>
    
        <tr>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; border-bottom: double; ">
    
        Waikiki
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">
    
        <i>Porites</i> spp.
    
        </td>
    
        <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; border-bottom: double; ">
    
        1.19 ±0.56 %
    
        </td>
    
        </tr>
    
        </table>